在Zend 1.12中使用Composer自动加载器(用于加载外部库)?

问题描述:

Basically, I'd want to use Composer auto-loader (for loading third-party libraries), but I want to continue using built-in mechanism for auto-loading in Zend 1.12

I added the following piece of code:

<?php // File path: index.php 

// ...

$composerAutoloaderPaths = array(
    '../vendor/autoload.php',
    '../../common/vendor/autoload.php' // store common libraries used by multiple projects, currently that's working by adding the directory in set_include_path()
);

foreach($composerAutoloaderPaths as $composerAutoloaderPath)
{
    if(file_exists($composerAutoloaderPath))
    {
        require_once $composerAutoloaderPath;
    }
    else 
    {
        // handle the error gracefully
    }
}

// ...

Also, I'm using Zend_Loader_Autoloader like this:

<?php // File path: Bootstrap.php 

// ...

$autoloader = Zend_Loader_Autoloader::getInstance();

$autoloader->registerNamespace('Plugin_');
$autoloader->registerNamespace('Helper_');
// etc.

// ...

Is there something to worry about using Composer and Zend autoloaders like this?

基本上,我想使用Composer自动加载器(用于加载第三方库),但我想要 继续在Zend 1.12中使用内置机制进行自动加载 p>

我添加了以下代码: p>

 &lt;?  php //文件路径:index.php 
 
 // ... 
 
 $ composerAutoloaderPaths = array(
'../vendor/autoload.php',
'../../common  /vendor/autoload.php'//存储多个项目使用的公共库,目前通过在set_include_path()
)中添加目录来工作; 
 
foreach($ composerAutoloaderPaths as $ composerAutoloaderPath)
 {
 if(  file_exists($ composerAutoloaderPath))
 {
 require_once $ composerAutoloaderPath; 
} 
 else 
 {
 //正常处理错误
} 
} 
 
 // ... 
   pre> 
 
 

另外,我正在使用 Zend_Loader_Autoloader code>,如下所示: p>

 &lt;?php /  /文件路径:Bootstrap.php 
 
 // ... 
 
 $ autoloader = Zend_Loader_Autoloader:  :getInstance(); 
 
 $ autoloader-&gt; registerNamespace('Plugin _'); 
 $ autoloader-&gt; registerNamespace('Helper _'); 
 //等等
 
 // ...  
  code>  pre> 
 
 

使用像这样的Composer和Zend自动加载器有什么值得担心的吗? p> div>

I often encounter this problem and I believe it's not an actual problem.

The best way IMO is to just include the composer autoloader in public/index.php as it's done in ZF2/3. This will not change a thing about the rest of the autoloading : https://github.com/zendframework/ZendSkeletonApplication/blob/master/public/index.php#L21

Beware: if you used another entry point in your application (for cron scripts for instance), you'd need to add the same lines (basically in each entrypoint of your application).

Also, if you look at the rules in phpmd, one gives this message:

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both.

Therefore, declaring the include of the vendor autoloader in the bootstrap could be considered as a malpractice (at least seems to be a shared opinion between whoever wrote this rule and myself :)).

You can autoload vendor in bootstrap.php like that: <?php // File path: Bootstrap.php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { /** * Inits Vendor */ protected function _initVendor() { require_once APPLICATION_PATH . '/../vendor/autoload.php'; // require_once APPLICATION_PATH . '/new/path/autoload.php'; } ... autoload whatever you want with Zend 1 ... }

I have to advise you to use is_file() instead of file_exists() because file_exists returns true when directory exists, but not necessary when .php file exists

I have to admire your resilience with ZF1, we all have been there 10 years ago. Zend Framework 1.x is full of require_once within its classes. You can require_once another file anytime on Bootstrap.php file anytime