| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Foundation; |
| 4 |
|
| 5 |
trait FacadeLoaderTrait |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Register facade/alias loader |
| 9 |
* @return void |
| 10 |
*/ |
| 11 |
protected function registerAppFacadeLoader() |
| 12 |
{ |
| 13 |
$this->isFacadeLoaderRegistered = true; |
| 14 |
spl_autoload_register([$this, 'aliasLoader'], true, false); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Facad/Alias loader |
| 19 |
* @param [type] $class [description] |
| 20 |
* @return [type] [description] |
| 21 |
*/ |
| 22 |
protected function aliasLoader($class) |
| 23 |
{ |
| 24 |
$namespace = $this->getNamespace(); |
| 25 |
if ($namespace == substr($class, 0, strlen($namespace))) { |
| 26 |
$parts = explode('\\', $class); |
| 27 |
if (count($parts) == 2) { |
| 28 |
if (array_key_exists($parts[1], static::$container['facades'])) { |
| 29 |
$containerKey = static::$container['facades'][$facade = $parts[1]]; |
| 30 |
$path = $this->storagePath().'framework/facades/'; |
| 31 |
if (!file_exists($file = $path.$facade.'.php')) { |
| 32 |
if (!file_exists($path)) { |
| 33 |
mkdir($path, 0777, true); |
| 34 |
} |
| 35 |
file_put_contents($file, $this->getFileData($facade, $containerKey)); |
| 36 |
} |
| 37 |
include $file; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Retrieve facade file content |
| 45 |
* @param string $alias |
| 46 |
* @param string $key |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
protected function getFileData($alias, $key) |
| 50 |
{ |
| 51 |
return str_replace( |
| 52 |
['DummyNamespace', 'DummyClass', 'DummyKey'], |
| 53 |
[$this->getNamespace(), $alias, $key], |
| 54 |
file_get_contents($this->frameworkPath().'Foundation/Facade.stub') |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|