vendor/contao/core-bundle/src/Resources/contao/library/Contao/ClassLoader.php line 234

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. /**
  11.  * Automatically loads class files based on a mapper array
  12.  *
  13.  * The class stores namespaces and classes and automatically loads the class
  14.  * files upon their first usage. It uses a mapper array to support complex
  15.  * nesting and arbitrary subfolders to store the class files in.
  16.  *
  17.  * Usage:
  18.  *
  19.  *     ClassLoader::addNamespace('Custom');
  20.  *     ClassLoader::addClass('Custom\Calendar', 'calendar/Calendar.php');
  21.  *
  22.  * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  23.  *             Use the Composer autoloader instead.
  24.  */
  25. class ClassLoader
  26. {
  27.     /**
  28.      * Known namespaces
  29.      * @var array
  30.      */
  31.     protected static $namespaces = array('Contao');
  32.     /**
  33.      * Known classes
  34.      * @var array
  35.      */
  36.     protected static $classes = array();
  37.     /**
  38.      * Add a new namespace
  39.      *
  40.      * @param string $name The namespace name
  41.      *
  42.      * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  43.      */
  44.     public static function addNamespace($name)
  45.     {
  46.         trigger_deprecation('contao/core-bundle''4.2''Using "Contao\ClassLoader::addNamespace()" has been deprecated and will no longer work in Contao 5.0.');
  47.         if (\in_array($nameself::$namespaces))
  48.         {
  49.             return;
  50.         }
  51.         array_unshift(self::$namespaces$name);
  52.     }
  53.     /**
  54.      * Add multiple new namespaces
  55.      *
  56.      * @param array $names An array of namespace names
  57.      *
  58.      * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  59.      */
  60.     public static function addNamespaces($names)
  61.     {
  62.         trigger_deprecation('contao/core-bundle''4.2''Using "Contao\ClassLoader::addNamespaces()" has been deprecated and will no longer work in Contao 5.0.');
  63.         foreach ($names as $name)
  64.         {
  65.             self::addNamespace($name);
  66.         }
  67.     }
  68.     /**
  69.      * Return the namespaces as array
  70.      *
  71.      * @return array An array of all namespaces
  72.      *
  73.      * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  74.      */
  75.     public static function getNamespaces()
  76.     {
  77.         trigger_deprecation('contao/core-bundle''4.2''Using "Contao\ClassLoader::getNamespaces()" has been deprecated and will no longer work in Contao 5.0.');
  78.         return self::$namespaces;
  79.     }
  80.     /**
  81.      * Add a new class with its file path
  82.      *
  83.      * @param string $class The class name
  84.      * @param string $file  The path to the class file
  85.      *
  86.      * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  87.      */
  88.     public static function addClass($class$file)
  89.     {
  90.         trigger_deprecation('contao/core-bundle''4.2''Using "Contao\ClassLoader::addClass()" has been deprecated and will no longer work in Contao 5.0.');
  91.         self::$classes[$class] = $file;
  92.     }
  93.     /**
  94.      * Add multiple new classes with their file paths
  95.      *
  96.      * @param array $classes An array of classes
  97.      *
  98.      * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  99.      */
  100.     public static function addClasses($classes)
  101.     {
  102.         trigger_deprecation('contao/core-bundle''4.2''Using "Contao\ClassLoader::addClasses()" has been deprecated and will no longer work in Contao 5.0.');
  103.         foreach ($classes as $class=>$file)
  104.         {
  105.             self::addClass($class$file);
  106.         }
  107.     }
  108.     /**
  109.      * Return the classes as array.
  110.      *
  111.      * @return array An array of all classes
  112.      *
  113.      * @deprecated Deprecated since Contao 4.2, to be removed in Contao 5.
  114.      */
  115.     public static function getClasses()
  116.     {
  117.         trigger_deprecation('contao/core-bundle''4.2''Using "Contao\ClassLoader::getClasses()" has been deprecated and will no longer work in Contao 5.0.');
  118.         return self::$classes;
  119.     }
  120.     /**
  121.      * Autoload a class and create an alias in the global namespace
  122.      *
  123.      * To preserve backwards compatibility with Contao 2 extensions, all core
  124.      * classes will be aliased into the global namespace.
  125.      *
  126.      * @param string $class The class name
  127.      */
  128.     public static function load($class)
  129.     {
  130.         if (class_exists($classfalse) || interface_exists($classfalse) || trait_exists($classfalse))
  131.         {
  132.             return;
  133.         }
  134.         // The class file is set in the mapper
  135.         if (isset(self::$classes[$class]))
  136.         {
  137.             if (System::getContainer()->getParameter('kernel.debug'))
  138.             {
  139.                 $GLOBALS['TL_DEBUG']['classes_set'][$class] = $class;
  140.             }
  141.             include System::getContainer()->getParameter('kernel.project_dir') . '/' self::$classes[$class];
  142.         }
  143.         // Find the class in the registered namespaces
  144.         elseif (($namespaced self::findClass($class)) !== null)
  145.         {
  146.             if (!class_exists($namespacedfalse) && !interface_exists($namespacedfalse) && !trait_exists($namespacedfalse))
  147.             {
  148.                 if (System::getContainer()->getParameter('kernel.debug'))
  149.                 {
  150.                     $GLOBALS['TL_DEBUG']['classes_aliased'][$class] = $namespaced;
  151.                 }
  152.                 include System::getContainer()->getParameter('kernel.project_dir') . '/' self::$classes[$namespaced];
  153.             }
  154.             class_alias($namespaced$class);
  155.         }
  156.         // Try to map the class to a Contao class loaded via Composer
  157.         elseif (strncmp($class'Contao\\'7) !== 0)
  158.         {
  159.             $namespaced 'Contao\\' $class;
  160.             if (class_exists($namespaced) || interface_exists($namespaced) || trait_exists($namespaced))
  161.             {
  162.                 if (System::getContainer()->getParameter('kernel.debug'))
  163.                 {
  164.                     $GLOBALS['TL_DEBUG']['classes_composerized'][$class] = $namespaced;
  165.                 }
  166.                 if (!class_exists($classfalse) && !interface_exists($classfalse) && !trait_exists($classfalse))
  167.                 {
  168.                     class_alias($namespaced$class);
  169.                 }
  170.             }
  171.         }
  172.         // Pass the request to other autoloaders (e.g. Swift)
  173.     }
  174.     /**
  175.      * Search the namespaces for a matching entry
  176.      *
  177.      * @param string $class The class name
  178.      *
  179.      * @return string|null The full path including the namespace or null
  180.      */
  181.     protected static function findClass($class)
  182.     {
  183.         foreach (self::$namespaces as $namespace)
  184.         {
  185.             if (isset(self::$classes[$namespace '\\' $class]))
  186.             {
  187.                 return $namespace '\\' $class;
  188.             }
  189.         }
  190.         return null;
  191.     }
  192.     /**
  193.      * Register the autoloader
  194.      */
  195.     public static function register()
  196.     {
  197.         spl_autoload_register('ClassLoader::load');
  198.     }
  199.     /**
  200.      * Scan the module directories for config/autoload.php files and then
  201.      * register the autoloader on the SPL stack
  202.      */
  203.     public static function scanAndRegister()
  204.     {
  205.         $strCacheDir System::getContainer()->getParameter('kernel.cache_dir');
  206.         // Try to load from cache
  207.         if (file_exists($strCacheDir '/contao/config/autoload.php'))
  208.         {
  209.             include $strCacheDir '/contao/config/autoload.php';
  210.         }
  211.         else
  212.         {
  213.             try
  214.             {
  215.                 $files System::getContainer()->get('contao.resource_locator')->locate('config/autoload.php'nullfalse);
  216.             }
  217.             catch (\InvalidArgumentException $e)
  218.             {
  219.                 $files = array();
  220.             }
  221.             foreach ($files as $file)
  222.             {
  223.                 include $file;
  224.             }
  225.         }
  226.         self::register();
  227.     }
  228. }
  229. class_alias(ClassLoader::class, 'ClassLoader');