Created
October 3, 2015 13:01
-
-
Save asaokamei/751d063531a0deb6f465 to your computer and use it in GitHub Desktop.
a generic app builder for PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AppBuilder | |
{ | |
/** | |
* @var mixed | |
*/ | |
public $app; | |
/** | |
* @var string main config dir (ex: project-root/app/) | |
*/ | |
public $config_dir; | |
/** | |
* @var string var dir (no version control) (ex: project-root/vars) | |
*/ | |
public $var_dir; | |
/** | |
* @var array list of environment | |
*/ | |
public $environment = ['production']; | |
/** | |
* @var bool debug or not | |
*/ | |
public $debug = false; | |
/** | |
* @var array | |
*/ | |
private $container = []; | |
/** | |
* @param mixed $app | |
*/ | |
public function __construct($app) | |
{ | |
$this->app = $app; | |
} | |
/** | |
* @param callable $callable | |
* @return $this | |
*/ | |
public function setup(callable $callable) | |
{ | |
$callable($this); | |
return $this; | |
} | |
/** | |
* caches entire Application, $app, to a file. | |
* specify $closure to construct the application in case cache file is absent. | |
* | |
* @param \Closure $closure | |
* @return $this | |
*/ | |
public function cached($closure) | |
{ | |
$cached = $this->var_dir . '/app.cached'; | |
if (!$this->debug && file_exists($cached)) { | |
$this->app = unserialize(\file_get_contents($cached)); | |
return $this; | |
} | |
$closure($this); | |
if (!$this->debug && !file_exists($cached)) { | |
\file_put_contents($cached, serialize($this->app)); | |
chmod($cached, 0666); | |
} | |
return $this; | |
} | |
/** | |
* @param string $name | |
* @param array $data | |
* @return $this | |
*/ | |
public function configure($name = null, $data = []) | |
{ | |
$dir = $this->config_dir . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR; | |
$environments = $this->debug ? ['config', 'config-debug'] : ['config']; | |
$environments = array_merge($environments, $this->environment); | |
foreach($environments as $env) { | |
$this->evaluatePhp($dir . $env, $data); | |
} | |
return $this; | |
} | |
/** | |
* @param string $name | |
* @param array $data | |
* @return mixed|null | |
*/ | |
public function evaluate($name, $data = []) | |
{ | |
$file = $this->config_dir . DIRECTORY_SEPARATOR . $name; | |
return $this->evaluatePhp($name, $data); | |
} | |
/** | |
* @param string $__config | |
* @param array $__data | |
* @return mixed|null | |
*/ | |
private function evaluatePhp($__config, $__data = []) | |
{ | |
$__file = $__config . '.php'; | |
if (!file_exists($__file)) { | |
throw new \InvalidArgumentException('Cannot find configuration file: ' . $__config); | |
} | |
if (file_exists($__file)) { | |
extract($__data); | |
/** @noinspection PhpUnusedLocalVariableInspection */ | |
$app = $this->app; | |
/** @noinspection PhpUnusedLocalVariableInspection */ | |
$builder = $this; | |
/** @noinspection PhpIncludeInspection */ | |
return include($__file); | |
} | |
return null; | |
} | |
/** | |
* loads the environment based configuration. | |
* | |
* @param string $env_file | |
* @return $this | |
*/ | |
public function loadEnvironment($env_file) | |
{ | |
$env_file .= '.php'; | |
if (!file_exists($env_file)) { | |
return $this; | |
} | |
/** @noinspection PhpIncludeInspection */ | |
$this->environment = (array)include($env_file); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment