GaryJones / Gamajo-Template-Loader

A class to copy into your WordPress plugin, to allow loading template parts with fallback through the child theme > parent theme > plugin.
GNU General Public License v2.0
292 stars 61 forks source link

Mozart-created class non-functional #38

Open leewillis77 opened 4 years ago

leewillis77 commented 4 years ago

Installing using mozart as recommended in the docs results in the following code which means that if there is a non-prefixed version somewhere in the software, the prefixed class doesn't get defined:

if ( ! class_exists( 'Gamajo_Template_Loader' ) ) {

    class MyPrefix_Gamajo_Template_Loader {

Attempting to add a class that extends MyPrefix_Gamajo_Template_Loader on a software stack that already includes a non-prefixed version of Gamajo_Template_Loader results in :

Fatal error: Class 'MyPrefix_Gamajo_Template_Loader' not found in /src/TemplateLoader.php

If all non-prefix instances are removed then this code works as expected.

MikeiLL commented 4 years ago

I'm confused about how to install this using Mozart.

This is my composer.json file:

#abbreviated

    "type": "wordpress-plugin",
    "require": {
        "php": ">=7.1",
        "composer/installers": "~1.0",
        "gamajo/template-loader": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "Cool_Plugin\\": "src"
        }
    },
    "extra": {
        "mozart": {
            "dep_namespace": "Cool_Plugin\\Dependencies\\",
            "dep_directory": "/src/Dependencies/",
            "classmap_directory": "/classes/dependencies/",
            "classmap_prefix": "Cool_Plugin",
            "delete_vendor_directories": true,
            "packages": [
                "gamajo/template-loader"
            ]
        }
    },
  "require-dev": {
    "coenjacobs/mozart": "dev-master"
  }
}

I have tried with and without the "packages" array. The only thing I see in /src/Dependencies is composer installers.

In the vendor directory there is:

vendor/gamajo/
└── template-loader
    ├── CHANGELOG.md
    ├── LICENSE
    ├── README.md
    ├── class-gamajo-template-loader.php
    └── composer.json

No namespaces in the class-gamajo-template-loader.php file.

Previously I have added this (fantastic) tool manually, but am hoping to do it the recommended way this time.

Any help appreciated.

Update

In the meantime, I'm using the files autoloading:


    "autoload": {
        "psr-4": {
            "MZ_Mobilize_America\\": "src"
        },
        "files": ["vendor/gamajo/template-loader/class-gamajo-template-loader.php"]

Then within my namespace I have to escape so php can find it in the global namespace:

namespace Cool_Plugin\Libraries;

use Cool_Plugin as NS;

class Template_Loader extends \Gamajo_Template_Loader {

    protected $filter_prefix = 'cool_plugin';

    protected $theme_template_directory = 'templates/cool_plugin';

    protected $plugin_directory = NS\PLUGIN_NAME_DIR; 
    // From main plugin file: define( NS . 'PLUGIN_NAME_DIR', plugin_dir_path( __FILE__ ) );

    protected $plugin_template_directory = 'src/Frontend/views';