自动加载器(Universal Class Loader)

Phalcon\Loader 组件允许你定义类的自动加载则规。

该组件基于 PHP 的 spl_autoload_register 函数实现尚未被定义的类(class)和接口(interface)的自动加载 autoloading classes。利用该组件实现 lazy initialization。我们可以从其他项目或者第三方库加载文件,兼容 PSR-0 and PSR-4 标准.

Phalcon\Loader 提供了 4 种加载方式,可以组合使用它们。

注册命名空间(Registering Namespaces)

当我们使用命名空间组织代码或者使用带有命名空间的外部库时,可以使用 registerNamespaces 提供的加载机制。它的值是一个关联数组,键对应命名空间前缀,值是对应的目录。当加载程序试图通过命名空间找到类时,命名空间的分隔符将被目录分隔符替换,然后作为查找的文件路径。

  1. <?php
  2.  
  3. use Phalcon\Loader;
  4.  
  5. // Creates the autoloader
  6. $loader = new Loader();
  7.  
  8. // Register some namespaces
  9. $loader->registerNamespaces(
  10. array(
  11. "Example\Base" => "vendor/example/base/",
  12. "Example\Adapter" => "vendor/example/adapter/",
  13. "Example" => "vendor/example/"
  14. )
  15. );
  16.  
  17. // Register autoloader
  18. $loader->register();
  19.  
  20. // The required class will automatically include the
  21. // file vendor/example/adapter/Some.php
  22. $some = new Example\Adapter\Some();

注册前缀(Registering Prefixes)

这个策略类似于命名空间策略。它需要一个关联数组,其中键是前缀,它们的值是类所在的目录。当加载程序试图通过命名空间找到类时,命名空间的分隔符和下划线 “_” 将被目录分隔符替换。

  1. <?php
  2.  
  3. use Phalcon\Loader;
  4.  
  5. // Creates the autoloader
  6. $loader = new Loader();
  7.  
  8. // Register some prefixes
  9. $loader->registerPrefixes(
  10. array(
  11. "Example_Base" => "vendor/example/base/",
  12. "Example_Adapter" => "vendor/example/adapter/",
  13. "Example_" => "vendor/example/"
  14. )
  15. );
  16.  
  17. // Register autoloader
  18. $loader->register();
  19.  
  20. // The required class will automatically include the
  21. // file vendor/example/adapter/Some.php
  22. $some = new Example_Adapter_Some();

注册文件夹(Registering Directories)

在文件目录多时不推荐该方法,它按照目录在数组内的顺序从上到下,查找文件。

  1. <?php
  2.  
  3. use Phalcon\Loader;
  4.  
  5. // Creates the autoloader
  6. $loader = new Loader();
  7.  
  8. // Register some directories
  9. $loader->registerDirs(
  10. array(
  11. "library/MyComponent/",
  12. "library/OtherComponent/Other/",
  13. "vendor/example/adapters/",
  14. "vendor/example/"
  15. )
  16. );
  17.  
  18. // Register autoloader
  19. $loader->register();
  20.  
  21. // The required class will automatically include the file from
  22. // the first directory where it has been located
  23. // i.e. library/OtherComponent/Other/Some.php
  24. $some = new Some();

注册类名(Registering Classes)

最后一个方式,是直接将类名和文件对应起来,这是性能最好的加载方式,但类的列表不太好维护,所以不推荐使用。

  1. <?php
  2.  
  3. use Phalcon\Loader;
  4.  
  5. // Creates the autoloader
  6. $loader = new Loader();
  7.  
  8. // Register some classes
  9. $loader->registerClasses(
  10. array(
  11. "Some" => "library/OtherComponent/Other/Some.php",
  12. "Example\Base" => "vendor/example/adapters/Example/BaseClass.php"
  13. )
  14. );
  15.  
  16. // Register autoloader
  17. $loader->register();
  18.  
  19. // Requiring a class will automatically include the file it references
  20. // in the associative array
  21. // i.e. library/OtherComponent/Other/Some.php
  22. $some = new Some();

额外的扩展名(Additional file extensions)

Some autoloading strategies such as “prefixes”, “namespaces” or “directories” automatically append the “php” extension at the end of the checked file. If youare using additional extensions you could set it with the method “setExtensions”. Files are checked in the order as it were defined:

  1. <?php
  2.  
  3. // Creates the autoloader
  4. $loader = new \Phalcon\Loader();
  5.  
  6. // Set file extensions to check
  7. $loader->setExtensions(array("php", "inc", "phb"));

修改当前策略(Modifying current strategies)

Additional auto-loading data can be added to existing values in the following way:

  1. <?php
  2.  
  3. // Adding more directories
  4. $loader->registerDirs(
  5. array(
  6. "../app/library/",
  7. "../app/plugins/"
  8. ),
  9. true
  10. );

Passing “true” as second parameter will merge the current values with new ones in any strategy.

安全层(Security Layer)

Phalcon\Loader offers a security layer sanitizing by default class names avoiding possible inclusion of unauthorized files.Consider the following example:

  1. <?php
  2.  
  3. // Basic autoloader
  4. spl_autoload_register(function ($className) {
  5. if (file_exists($className . '.php')) {
  6. require $className . '.php';
  7. }
  8. });

The above auto-loader lacks of any security check, if by mistake in a function that launch the auto-loader,a malicious prepared string is used as parameter this would allow to execute any file accessible by the application:

  1. <?php
  2.  
  3. // This variable is not filtered and comes from an insecure source
  4. $className = '../processes/important-process';
  5.  
  6. // Check if the class exists triggering the auto-loader
  7. if (class_exists($className)) {
  8. // ...
  9. }

If ‘../processes/important-process.php’ is a valid file, an external user could execute the file withoutauthorization.

To avoid these or most sophisticated attacks, Phalcon\Loader removes any invalid character from the class namereducing the possibility of being attacked.

自动加载事件(Autoloading Events)

In the following example, the EventsManager is working with the class loader, allowing us to obtain debugging information regarding the flow of operation:

  1. <?php
  2.  
  3. $eventsManager = new \Phalcon\Events\Manager();
  4.  
  5. $loader = new \Phalcon\Loader();
  6.  
  7. $loader->registerNamespaces(
  8. array(
  9. 'Example\\Base' => 'vendor/example/base/',
  10. 'Example\\Adapter' => 'vendor/example/adapter/',
  11. 'Example' => 'vendor/example/'
  12. )
  13. );
  14.  
  15. // Listen all the loader events
  16. $eventsManager->attach('loader', function ($event, $loader) {
  17. if ($event->getType() == 'beforeCheckPath') {
  18. echo $loader->getCheckedPath();
  19. }
  20. });
  21.  
  22. $loader->setEventsManager($eventsManager);
  23.  
  24. $loader->register();

Some events when returning boolean false could stop the active operation. The following events are supported:

Event Name Triggered Can stop operation?
beforeCheckClass Triggered before starting the autoloading process Yes
pathFound Triggered when the loader locate a class No
afterCheckClass Triggered after finish the autoloading process. If this event is launched the autoloader didn’t find the class file No

注意事项(Troubleshooting)

Some things to keep in mind when using the universal autoloader:

  • Auto-loading process is case-sensitive, the class will be loaded as it is written in the code
  • Strategies based on namespaces/prefixes are faster than the directories strategy
  • If a cache bytecode like APC is installed this will used to retrieve the requested file (an implicit caching of the file is performed)

原文: http://www.myleftstudio.com/reference/loader.html