Skip to content

Commit

Permalink
improve facade loading
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 30, 2016
1 parent 77d034f commit feb52bf
Showing 3 changed files with 118 additions and 10 deletions.
76 changes: 76 additions & 0 deletions src/Illuminate/Foundation/AliasLoader.php
Original file line number Diff line number Diff line change
@@ -18,6 +18,13 @@ class AliasLoader
*/
protected $registered = false;

/**
* The namespace for all real-time facades.
*
* @var string
*/
protected static $facadeNamespace = 'Facades\\';

/**
* The singleton instance of the loader.
*
@@ -62,11 +69,69 @@ public static function getInstance(array $aliases = [])
*/
public function load($alias)
{
if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) {
$this->loadFacade($alias);

return true;
}

if (isset($this->aliases[$alias])) {
return class_alias($this->aliases[$alias], $alias);
}
}

/**
* Load a real-time facade for the given alias.
*
* @param string $alias
* @return bool
*/
protected function loadFacade($alias)
{
tap($this->ensureFacadeExists($alias), function ($path) {
require $path;
});
}

/**
* Ensure that the given alias has an existing real-time facade class.
*
* @param string $class
* @return string
*/
protected function ensureFacadeExists($alias)
{
if (file_exists($path = base_path('bootstrap/cache/facade-'.sha1($alias).'.php'))) {
return $path;
}

file_put_contents($path, $this->formatFacadeStub(
$alias, file_get_contents(__DIR__.'/stubs/facade.stub')
));

return $path;
}

/**
* Format the facade stub with the proper namespace and class.
*
* @param string $alias
* @param string $stub
* @return string
*/
protected function formatFacadeStub($alias, $stub)
{
$replacements = [
str_replace('/', '\\', dirname(str_replace('\\', '/', $alias))),
class_basename($alias),
substr($alias, strlen(static::$facadeNamespace)),
];

return str_replace(
['DummyNamespace', 'DummyClass', 'DummyTarget'], $replacements, $stub
);
}

/**
* Add an alias to the loader.
*
@@ -145,6 +210,17 @@ public function setRegistered($value)
$this->registered = $value;
}

/**
* Set the real-time facade namespace.
*
* @param string $namespace
* @return void
*/
public static function setFacadeNamespace($namespace)
{
static::$facadeNamespace = rtrim($namespace, '\\').'\\';
}

/**
* Set the value of the singleton alias loader.
*
31 changes: 21 additions & 10 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
@@ -820,6 +820,16 @@ public function shouldSkipMiddleware()
$this->make('middleware.disable') === true;
}

/**
* Get the path to the cached services.php file.
*
* @return string
*/
public function getCachedServicesPath()
{
return $this->bootstrapPath().'/cache/services.php';
}

/**
* Determine if the application configuration is cached.
*
@@ -860,16 +870,6 @@ public function getCachedRoutesPath()
return $this->bootstrapPath().'/cache/routes.php';
}

/**
* Get the path to the cached services.php file.
*
* @return string
*/
public function getCachedServicesPath()
{
return $this->bootstrapPath().'/cache/services.php';
}

/**
* Determine if the application is currently down for maintenance.
*
@@ -977,6 +977,17 @@ public function isDeferredService($service)
return isset($this->deferredServices[$service]);
}

/**
* Configure the real-time facade namespace.
*
* @param string $namespace
* @return void
*/
public function provideFacades($namespace)
{
AliasLoader::setFacadeNamespace($namespace);
}

/**
* Define a callback to be used to configure Monolog.
*
21 changes: 21 additions & 0 deletions src/Illuminate/Foundation/stubs/facade.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DummyNamespace;

use Illuminate\Support\Facades\Facade;

/**
* @see \DummyTarget
*/
class DummyClass extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'DummyTarget';
}
}

0 comments on commit feb52bf

Please sign in to comment.