ApcUniversalClassLoader.php

Namespace

Symfony\Component\ClassLoader

Classes

Namesort descending Description
ApcUniversalClassLoader ApcUniversalClassLoader implements a "universal" autoloader cached in APC for PHP 5.3.

File

Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
View source
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader;
  11. /**
  12. * ApcUniversalClassLoader implements a "universal" autoloader cached in APC for PHP 5.3.
  13. *
  14. * It is able to load classes that use either:
  15. *
  16. * * The technical interoperability standards for PHP 5.3 namespaces and
  17. * class names (http://groups.google.com/group/php-standards/web/psr-0-final-proposal);
  18. *
  19. * * The PEAR naming convention for classes (http://pear.php.net/).
  20. *
  21. * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
  22. * looked for in a list of locations to ease the vendoring of a sub-set of
  23. * classes for large projects.
  24. *
  25. * Example usage:
  26. *
  27. * require 'vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
  28. * require 'vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';
  29. *
  30. * use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
  31. *
  32. * $loader = new ApcUniversalClassLoader('apc.prefix.');
  33. *
  34. * // register classes with namespaces
  35. * $loader->registerNamespaces(array(
  36. * 'Symfony\Component' => __DIR__.'/component',
  37. * 'Symfony' => __DIR__.'/framework',
  38. * 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
  39. * ));
  40. *
  41. * // register a library using the PEAR naming convention
  42. * $loader->registerPrefixes(array(
  43. * 'Swift_' => __DIR__.'/Swift',
  44. * ));
  45. *
  46. * // activate the autoloader
  47. * $loader->register();
  48. *
  49. * In this example, if you try to use a class in the Symfony\Component
  50. * namespace or one of its children (Symfony\Component\Console for instance),
  51. * the autoloader will first look for the class under the component/
  52. * directory, and it will then fallback to the framework/ directory if not
  53. * found before giving up.
  54. *
  55. * @author Fabien Potencier <fabien@symfony.com>
  56. * @author Kris Wallsmith <kris@symfony.com>
  57. *
  58. * @api
  59. */
  60. class ApcUniversalClassLoader extends UniversalClassLoader
  61. {
  62. private $prefix;
  63. /**
  64. * Constructor.
  65. *
  66. * @param string $prefix A prefix to create a namespace in APC
  67. *
  68. * @api
  69. */
  70. public function __construct($prefix)
  71. {
  72. if (!extension_loaded('apc')) {
  73. throw new \RuntimeException('Unable to use ApcUniversalClassLoader as APC is not enabled.');
  74. }
  75. $this->prefix = $prefix;
  76. }
  77. /**
  78. * Finds a file by class name while caching lookups to APC.
  79. *
  80. * @param string $class A class name to resolve to file
  81. */
  82. public function findFile($class)
  83. {
  84. if (false === $file = apc_fetch($this->prefix.$class)) {
  85. apc_store($this->prefix.$class, $file = parent::findFile($class));
  86. }
  87. return $file;
  88. }
  89. }