{"id":229545,"date":"2022-11-04T16:59:00","date_gmt":"2022-11-04T13:59:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=229545"},"modified":"2022-11-09T08:24:30","modified_gmt":"2022-11-09T05:24:30","slug":"rapid-prototyping-esittelyssae-automaattinen-lataus","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/fi\/rapid-prototyping-esittelyssae-automaattinen-lataus\/","title":{"rendered":"Rapid Prototyping: Esittelyss\u00e4 automaattinen lataus"},"content":{"rendered":"\n<p>Muutaman viime postauksen aikana olen k\u00e4ynyt l\u00e4pi prosessin vied\u00e4 idea ideasta prototyyppiin.<\/p>\n<p>My\u00f6nnett\u00e4k\u00f6\u00f6n, ett\u00e4 on joitain asioita, joita saatat muuttaa (ja on joitain asioita, joita muuttaisin koskien luokkien j\u00e4rjest\u00e4mist\u00e4). Mutta sarjan tarkoitus ei ole k\u00e4yd\u00e4 l\u00e4pi kaikkia erilaisia \u200b\u200boliotekniikoita, joita voidaan k\u00e4ytt\u00e4\u00e4 ratkaisun luomiseen.<\/p>\n<p>Sen sijaan kyse on prototyypin ottamisesta ja sen muuntamisesta joksikin hieman ammattimaisemmaksi. Yksi asia meid\u00e4n on kuitenkin viel\u00e4 teht\u00e4v\u00e4.<\/p>\n<p>T\u00e4ll\u00e4 hetkell\u00e4 sis\u00e4llyt\u00e4mme kaikki tiedostomme vaadittujen lausuntojen kautta. Ja t\u00e4m\u00e4 toimii hyvin pienille tiedostoille, mutta automaattisen latauksen k\u00e4ytt\u00f6\u00f6notto yhdess\u00e4 nimiavaruuksien kanssa voi tehd\u00e4 sovelluksesta entist\u00e4 puhtaamman.<\/p>\n<p>Ja niin me aiomme tehd\u00e4.<\/p>\n<h3>Esittelyss\u00e4 automaattinen lataus<\/h3>\n<p>Muista aiemmasta viestist\u00e4 (jos olet lukenut sen), ett\u00e4 automaattinen lataus on yksinkertainen idea:<\/p>\n<p>Kuulostaa hienolta, eik\u00f6? Mutta on varoitus: sinun on kirjoitettava automaattinen lataus. PHP ei voi selvitt\u00e4\u00e4 sit\u00e4 yksin.<\/p>\n<p>Ja t\u00e4st\u00e4 syyst\u00e4 pid\u00e4n siit\u00e4, ett\u00e4 nimiavaruksemme noudattavat my\u00f6s johdonmukaista hakemistorakennetta: Jos j\u00e4senn\u00e4t nimiavaruuden ja tiedoston nimen, on suhteellisen helppoa rakentaa tiedoston polku ja ladata se automaattisesti.<\/p>\n<p>Kuulostaako h\u00e4mment\u00e4v\u00e4lt\u00e4? Katsotaanpa ensin, mihin sijoitamme automaattisen latausohjelman hakemistorakenteen suhteen:<\/p>\n<p>P\u00e4ivit\u00e4 seuraavaksi laajennuksen bootstrap-tiedosto niin, ett\u00e4 se <a href=\"https:\/\/gist.github.com\/tommcfarlin\/4f5087613f01dcb35b7fb5e79c69401d#file-00-plugin-bootstrap-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">vaatii vain automaattisen lataustiedoston<\/a>.<\/p>\n<pre><code>&lt;?php\n\/**\n * Three Recent Posts\n *\n * @package     TRP\n * @author      Tom McFarlin\n * @copyright   2017 Tom McFarlin\n * @license     MIT\n *\n * @wordpress-plugin\n * Plugin Name: Three Recent Posts\n * Plugin URI:  https:\/\/tommcfarlin.com\/three-recent-posts\/\n * Description: Displays the three mot recent posts in your post editor screen.\n * Version:     1.0.0\n * Author:      Tom McFarlin\n * Author URI:  https:\/\/tommcfarlin.com\n * Text Domain: three-recent-posts\n * License:     GPL\n * License URI: http:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\n *\/\n\nnamespace McFarlinTRP;\nuse McFarlinTRPDisplayMeta_Box;\n\n\/\/ Import the autoloader.\ninclude_once 'Includes\/autoload.php';\n\nadd_action( 'add_meta_boxes', __NAMESPACE__. 'trp_start' );\n\/**\n * Starts the plugin.\n *\/\nfunction trp_start() {\n\n    $meta_box = new Meta_Box( dirname( __FILE__) );\n    $meta_box-&gt;init();\n}\n<\/code><\/pre>\n<p>Tietenkin tarvitsemme automaattisen latausohjelman tiedostojemme lataamiseen. Pohjimmiltaan se toimii seuraavasti:<\/p>\n<ol>\n<li>erota saapuvan tiedoston nimen osat (kuten hakemistopolku ja tiedoston nimi),<\/li>\n<li>korvaa kaikki alaviivat tavuviivoilla varmistaaksesi, ett\u00e4 tiedosto noudattaa luokan tiedostonimen rakennetta,<\/li>\n<li>aloita t\u00e4ydellisen polun nimen rakentaminen (joka on hakemisto, tiedoston nimi jne.),<\/li>\n<li>iteroi nimiavaruuden l\u00e4pi luokan nimeen asti ja rakenna tiedostolle t\u00e4ydellinen nimi,<\/li>\n<li>sis\u00e4llytt\u00e4\u00e4 se projektiin.<\/li>\n<\/ol>\n<p>Se on suoraan eteenp\u00e4in, eik\u00f6? Jos noudatat johdonmukaista nimiavaruutta, hakemistoa, luokan nime\u00e4 ja tiedostojen nime\u00e4misskeemaa, voit k\u00e4ytt\u00e4\u00e4 samaa automaattista latausohjelmaa uudelleen miss\u00e4 tahansa projektissa.<\/p>\n<p><a href=\"https:\/\/gist.github.com\/tommcfarlin\/4f5087613f01dcb35b7fb5e79c69401d#file-00-autoload-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">T\u00e4ss\u00e4 on kopio automaattisesta latausohjelmasta<\/a>, jota k\u00e4yt\u00e4n ja pudotan sen melkein kaikkiin k\u00e4ytt\u00e4miini projekteihin. Huomaat, ett\u00e4 se noudattaa t\u00e4sm\u00e4lleen sit\u00e4, mit\u00e4 yll\u00e4 on k\u00e4sitelty:<\/p>\n<pre><code>&lt;?php\n\/**\n * Automatically loads the specified file.\n *\n * @package McFarlinTFP\n *\/\n\nnamespace McFarlinTFP;\n\n\/**\n * Automatically loads the specified file.\n *\n * Examines the fully qualified class name, separates it into components, then creates\n * a string that represents where the file is loaded on disk.\n *\n * @package McFarlinTFP;\n *\/\nspl_autoload_register(function( $filename) {\n\n    \/\/ First, separate the components of the incoming file.\n    $file_path = explode( '', $filename );\n\n    \/\/ Get the last index of the array. This is the class we're loading.\n    if (isset( $file_path[ count( $file_path) - 1 ])) {\n\n        $class_file = strtolower(\n            $file_path[ count( $file_path) - 1 ]\n        );\n\n        \/**\n         * The classname has an underscore, so we need to replace it\n         * with a hyphen for the file name.\n         *\/\n        $class_file = str_ireplace( '_', '-', $class_file );\n        $class_file = \"class-$class_file.php\";\n    }\n\n    \/**\n     * Find the fully qualified path to the class file by iterating through the $file_path array.\n     * We ignore the first index since it's always the top-level package. The last index is always\n     * the file so we append that at the end.\n     *\/\n    $fully_qualified_path = trailingslashit(\n        dirname(\n            dirname( __FILE__)) );\n\n    \/**\n     * We start at the second index of the namespace because our directory\n     * structure does not include 'McFarlin\/TRP'.\n     *\/\n    for ($i = 2; $i &lt; count( $file_path) - 1; $i++) {\n\n        $dir = strtolower( $file_path[ $i ] );\n        $fully_qualified_path .= trailingslashit( $dir );\n    }\n    $fully_qualified_path .= $class_file;\n\n    \/\/ Now we include the file.\n    if (file_exists( $fully_qualified_path)) {\n        include_once( $fully_qualified_path );\n    }\n});<\/code><\/pre>\n<p>Ja t\u00e4m\u00e4n my\u00f6t\u00e4 laajennus on k\u00e4\u00e4ritty versioon 1.0.0. Voit <a href=\"https:\/\/github.com\/tommcfarlin\/three-recent-posts\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">tarkistaa sen GitHubista<\/a> (sek\u00e4 kirjaimellisesti ett\u00e4 vain arvostelua varten). Ja toivottavasti t\u00e4m\u00e4 on auttanut k\u00e4velem\u00e4\u00e4n prosessin l\u00e4pi siirtymisess\u00e4 nopeasta prototyypist\u00e4 konseptiin WordPressin konseptissa.<\/p>\n<h2>Sarjan postaukset<\/h2>\n<ol>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/fi\/nopea-prototyyppien-luominen-wordpressillae-konseptista-laajennukseen\/\" title=\"Nopea prototyyppien luominen WordPressill\u00e4: konseptista laajennukseen\">Nopea prototyyppien luominen WordPressill\u00e4: konseptista laajennukseen<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/fi\/nopea-prototyyppien-luominen-wordpressillae-konseptianalyysi\/\" title=\"Nopea prototyyppien luominen WordPressill\u00e4: konseptianalyysi\">Nopea prototyyppien luominen WordPressill\u00e4: konseptianalyysi<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/fi\/nopea-prototyyppi-prototype-to-code-osa-1\/\" title=\"Nopea prototyyppi: Prototype to Code, osa 1\">Nopea prototyyppi: Prototype to Code, osa 1<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/fi\/rapid-prototyping-prototype-to-code-osa-2\/\" title=\"Rapid Prototyping: Prototype to Code, osa 2\">Rapid Prototyping: Prototype to Code, osa 2<\/a><\/li>\n<li><a href=\"https:\/\/tommcfarlin.com\/introducing-autoloading\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Rapid Prototyping: Esittelyss\u00e4 automaattinen lataus<\/a><\/li>\n<\/ol>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automaattisen latauksen k\u00e4ytt\u00f6\u00f6notto yhdess\u00e4 nimiavaruuksien kanssa WordPress-laajennuksessa voi tehd\u00e4 sovelluksesta entist\u00e4 puhtaamman.<\/p>\n","protected":false},"author":1,"featured_media":220982,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[719,917,843],"tags":[1166],"class_list":["post-229545","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kehittaejae","category-muut","category-opetusohjelmia","tag-affiai-fi"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/posts\/229545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/comments?post=229545"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/posts\/229545\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/media\/220982"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/media?parent=229545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/categories?post=229545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fi\/wp-json\/wp\/v2\/tags?post=229545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}