"Fossies" - the Fresh Open Source Software Archive

Member "framework-8.67.0/src/Illuminate/Foundation/AliasLoader.php" (22 Oct 2021, 5155 Bytes) of package /linux/www/laravel-framework-8.67.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AliasLoader.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Illuminate\Foundation;
    4 
    5 class AliasLoader
    6 {
    7     /**
    8      * The array of class aliases.
    9      *
   10      * @var array
   11      */
   12     protected $aliases;
   13 
   14     /**
   15      * Indicates if a loader has been registered.
   16      *
   17      * @var bool
   18      */
   19     protected $registered = false;
   20 
   21     /**
   22      * The namespace for all real-time facades.
   23      *
   24      * @var string
   25      */
   26     protected static $facadeNamespace = 'Facades\\';
   27 
   28     /**
   29      * The singleton instance of the loader.
   30      *
   31      * @var \Illuminate\Foundation\AliasLoader
   32      */
   33     protected static $instance;
   34 
   35     /**
   36      * Create a new AliasLoader instance.
   37      *
   38      * @param  array  $aliases
   39      * @return void
   40      */
   41     private function __construct($aliases)
   42     {
   43         $this->aliases = $aliases;
   44     }
   45 
   46     /**
   47      * Get or create the singleton alias loader instance.
   48      *
   49      * @param  array  $aliases
   50      * @return \Illuminate\Foundation\AliasLoader
   51      */
   52     public static function getInstance(array $aliases = [])
   53     {
   54         if (is_null(static::$instance)) {
   55             return static::$instance = new static($aliases);
   56         }
   57 
   58         $aliases = array_merge(static::$instance->getAliases(), $aliases);
   59 
   60         static::$instance->setAliases($aliases);
   61 
   62         return static::$instance;
   63     }
   64 
   65     /**
   66      * Load a class alias if it is registered.
   67      *
   68      * @param  string  $alias
   69      * @return bool|null
   70      */
   71     public function load($alias)
   72     {
   73         if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) {
   74             $this->loadFacade($alias);
   75 
   76             return true;
   77         }
   78 
   79         if (isset($this->aliases[$alias])) {
   80             return class_alias($this->aliases[$alias], $alias);
   81         }
   82     }
   83 
   84     /**
   85      * Load a real-time facade for the given alias.
   86      *
   87      * @param  string  $alias
   88      * @return void
   89      */
   90     protected function loadFacade($alias)
   91     {
   92         require $this->ensureFacadeExists($alias);
   93     }
   94 
   95     /**
   96      * Ensure that the given alias has an existing real-time facade class.
   97      *
   98      * @param  string  $alias
   99      * @return string
  100      */
  101     protected function ensureFacadeExists($alias)
  102     {
  103         if (is_file($path = storage_path('framework/cache/facade-'.sha1($alias).'.php'))) {
  104             return $path;
  105         }
  106 
  107         file_put_contents($path, $this->formatFacadeStub(
  108             $alias, file_get_contents(__DIR__.'/stubs/facade.stub')
  109         ));
  110 
  111         return $path;
  112     }
  113 
  114     /**
  115      * Format the facade stub with the proper namespace and class.
  116      *
  117      * @param  string  $alias
  118      * @param  string  $stub
  119      * @return string
  120      */
  121     protected function formatFacadeStub($alias, $stub)
  122     {
  123         $replacements = [
  124             str_replace('/', '\\', dirname(str_replace('\\', '/', $alias))),
  125             class_basename($alias),
  126             substr($alias, strlen(static::$facadeNamespace)),
  127         ];
  128 
  129         return str_replace(
  130             ['DummyNamespace', 'DummyClass', 'DummyTarget'], $replacements, $stub
  131         );
  132     }
  133 
  134     /**
  135      * Add an alias to the loader.
  136      *
  137      * @param  string  $alias
  138      * @param  string  $class
  139      * @return void
  140      */
  141     public function alias($alias, $class)
  142     {
  143         $this->aliases[$alias] = $class;
  144     }
  145 
  146     /**
  147      * Register the loader on the auto-loader stack.
  148      *
  149      * @return void
  150      */
  151     public function register()
  152     {
  153         if (! $this->registered) {
  154             $this->prependToLoaderStack();
  155 
  156             $this->registered = true;
  157         }
  158     }
  159 
  160     /**
  161      * Prepend the load method to the auto-loader stack.
  162      *
  163      * @return void
  164      */
  165     protected function prependToLoaderStack()
  166     {
  167         spl_autoload_register([$this, 'load'], true, true);
  168     }
  169 
  170     /**
  171      * Get the registered aliases.
  172      *
  173      * @return array
  174      */
  175     public function getAliases()
  176     {
  177         return $this->aliases;
  178     }
  179 
  180     /**
  181      * Set the registered aliases.
  182      *
  183      * @param  array  $aliases
  184      * @return void
  185      */
  186     public function setAliases(array $aliases)
  187     {
  188         $this->aliases = $aliases;
  189     }
  190 
  191     /**
  192      * Indicates if the loader has been registered.
  193      *
  194      * @return bool
  195      */
  196     public function isRegistered()
  197     {
  198         return $this->registered;
  199     }
  200 
  201     /**
  202      * Set the "registered" state of the loader.
  203      *
  204      * @param  bool  $value
  205      * @return void
  206      */
  207     public function setRegistered($value)
  208     {
  209         $this->registered = $value;
  210     }
  211 
  212     /**
  213      * Set the real-time facade namespace.
  214      *
  215      * @param  string  $namespace
  216      * @return void
  217      */
  218     public static function setFacadeNamespace($namespace)
  219     {
  220         static::$facadeNamespace = rtrim($namespace, '\\').'\\';
  221     }
  222 
  223     /**
  224      * Set the value of the singleton alias loader.
  225      *
  226      * @param  \Illuminate\Foundation\AliasLoader  $loader
  227      * @return void
  228      */
  229     public static function setInstance($loader)
  230     {
  231         static::$instance = $loader;
  232     }
  233 
  234     /**
  235      * Clone method.
  236      *
  237      * @return void
  238      */
  239     private function __clone()
  240     {
  241         //
  242     }
  243 }