Drupal investigation

ApcUniversalClassLoader.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. @trigger_error('The '.__NAMESPACE__.'\ApcUniversalClassLoader class is deprecated since version 2.7 and will be removed in 3.0. Use the Symfony\Component\ClassLoader\ApcClassLoader class instead.', E_USER_DEPRECATED);
  12. /**
  13. * ApcUniversalClassLoader implements a "universal" autoloader cached in APC for PHP 5.3.
  14. *
  15. * It is able to load classes that use either:
  16. *
  17. * * The technical interoperability standards for PHP 5.3 namespaces and
  18. * class names (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md);
  19. *
  20. * * The PEAR naming convention for classes (http://pear.php.net/).
  21. *
  22. * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
  23. * looked for in a list of locations to ease the vendoring of a sub-set of
  24. * classes for large projects.
  25. *
  26. * Example usage:
  27. *
  28. * require 'vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
  29. * require 'vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';
  30. *
  31. * use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
  32. *
  33. * $loader = new ApcUniversalClassLoader('apc.prefix.');
  34. *
  35. * // register classes with namespaces
  36. * $loader->registerNamespaces(array(
  37. * 'Symfony\Component' => __DIR__.'/component',
  38. * 'Symfony' => __DIR__.'/framework',
  39. * 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
  40. * ));
  41. *
  42. * // register a library using the PEAR naming convention
  43. * $loader->registerPrefixes(array(
  44. * 'Swift_' => __DIR__.'/Swift',
  45. * ));
  46. *
  47. * // activate the autoloader
  48. * $loader->register();
  49. *
  50. * In this example, if you try to use a class in the Symfony\Component
  51. * namespace or one of its children (Symfony\Component\Console for instance),
  52. * the autoloader will first look for the class under the component/
  53. * directory, and it will then fallback to the framework/ directory if not
  54. * found before giving up.
  55. *
  56. * @author Fabien Potencier <fabien@symfony.com>
  57. * @author Kris Wallsmith <kris@symfony.com>
  58. *
  59. * @deprecated since version 2.4, to be removed in 3.0.
  60. * Use the {@link ClassLoader} class instead.
  61. */
  62. class ApcUniversalClassLoader extends UniversalClassLoader
  63. {
  64. private $prefix;
  65. /**
  66. * Constructor.
  67. *
  68. * @param string $prefix A prefix to create a namespace in APC
  69. *
  70. * @throws \RuntimeException
  71. */
  72. public function __construct($prefix)
  73. {
  74. if (!function_exists('apcu_fetch')) {
  75. throw new \RuntimeException('Unable to use ApcUniversalClassLoader as APC is not enabled.');
  76. }
  77. $this->prefix = $prefix;
  78. }
  79. /**
  80. * Finds a file by class name while caching lookups to APC.
  81. *
  82. * @param string $class A class name to resolve to file
  83. *
  84. * @return string|null The path, if found
  85. */
  86. public function findFile($class)
  87. {
  88. $file = apcu_fetch($this->prefix.$class, $success);
  89. if (!$success) {
  90. apcu_store($this->prefix.$class, $file = parent::findFile($class) ?: null);
  91. }
  92. return $file;
  93. }
  94. }