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

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