PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] CakePhp3.7 Plugin Autoload option is not available

 March 18, 2022     cakephp, cakephp-3.x     No comments   

Issue

Prior to CakePhp3.7 it was possible to load a plugin using the autoload option:

Plugin::load('ContactManager', ['autoload' => true]);

This was very useful if you couldn't (or didn't want to) use composer to autoload plugins.

Since version 3.7.0: Plugin::load() and autoload option are deprecated.

$this->addPlugin('ContactManager');

Should be used instead of Plugin::load. But autoload option is not available on addPlugin().

How can I replicate the autoload functionality in CakePhp3.7 without using composer?


Solution

Well, there isn't much you can do other than re-implementing/replicating what Plugin::load() does, that is registering an autoloader, see:

  • https://github.com/cakephp/cakephp/blob/3.7.8/src/Core/Plugin.php#L130-L142
  • https://github.com/cakephp/cakephp/blob/3.7.8/src/Core/Plugin.php#L157-L170

You could for example put it in your Application class:

use Cake\Core\ClassLoader;
use Cake\Core\Plugin;

// ...

class Application extends BaseApplication
{
    // ...

    protected static $_loader;

    public function bootstrap()
    {
        // ...

        $this->addPlugin('ContactManager', ['autoload' => true]);
    }

    public function addPlugin($name, array $config = [])
    {
        parent::addplugin($name, $config);

        $config += [
            'autoload' => false,
            'classBase' => 'src',
        ];

        if ($config['autoload'] === true) {
            if (!isset($config['path'])) {
                $config['path'] = Plugin::getCollection()->findPath($name);
            }

            if (empty(static::$_loader)) {
                static::$_loader = new ClassLoader();
                static::$_loader->register();
            }
            static::$_loader->addNamespace(
                str_replace('/', '\\', $name),
                $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR
            );
            static::$_loader->addNamespace(
                str_replace('/', '\\', $name) . '\Test',
                $config['path'] . 'tests' . DIRECTORY_SEPARATOR
            );
        }

        return $this;
    }

    // ...
}

For now \Cake\Core\ClassLoader isn't deprecated, but it might become at one point, so that you may have to re-implement that too.



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing