CakePhp3.7 插件自动加载选项不可用
CakePhp3.7 Plugin Autoload option is not available
在 CakePhp3.7 之前,可以使用自动加载选项加载插件:
Plugin::load('ContactManager', ['autoload' => true]);
如果您不能(或不想)使用 Composer 自动加载插件,这将非常有用。
自版本 3.7.0 起:Plugin::load() 和自动加载选项已弃用。
$this->addPlugin('ContactManager');
应使用 而不是 Plugin::load。但是自动加载选项在 addPlugin() 上不可用。
如何在不使用 composer 的情况下复制 CakePhp3.7 中的自动加载功能?
好吧,除了 re-implementing/replicating Plugin::load()
的功能之外,您无能为力,那就是注册自动加载器,请参阅:
- 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
例如,您可以将其放入 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;
}
// ...
}
目前 \Cake\Core\ClassLoader
没有被弃用,但它可能会在某一时刻被弃用,因此您可能也必须 re-implement。
在 CakePhp3.7 之前,可以使用自动加载选项加载插件:
Plugin::load('ContactManager', ['autoload' => true]);
如果您不能(或不想)使用 Composer 自动加载插件,这将非常有用。
自版本 3.7.0 起:Plugin::load() 和自动加载选项已弃用。
$this->addPlugin('ContactManager');
应使用 而不是 Plugin::load。但是自动加载选项在 addPlugin() 上不可用。
如何在不使用 composer 的情况下复制 CakePhp3.7 中的自动加载功能?
好吧,除了 re-implementing/replicating Plugin::load()
的功能之外,您无能为力,那就是注册自动加载器,请参阅:
- 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
例如,您可以将其放入 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;
}
// ...
}
目前 \Cake\Core\ClassLoader
没有被弃用,但它可能会在某一时刻被弃用,因此您可能也必须 re-implement。