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
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.