Skip to content

Commit

Permalink
Add Autoconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Feb 5, 2017
1 parent 25ca6a7 commit 267e8b4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 55 deletions.
19 changes: 10 additions & 9 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace ICanBoogie;

use ICanBoogie\Autoconfig\Autoconfig;

/*
* Core
*/
Expand Down Expand Up @@ -115,17 +117,16 @@ function get_autoconfig()
trigger_error("The autoconfig file is missing. Check the `script` section of your composer.json file. https://icanboogie.org/docs/4.0/autoconfig#generating-the-autoconfig-file", E_USER_ERROR);
}

$autoconfig = (require AUTOCONFIG_PATHNAME) + [

'app-root' => 'app'

];
$autoconfig = (require AUTOCONFIG_PATHNAME);

$root = $autoconfig['root'];
$autoconfig['app-root'] = realpath($root . DIRECTORY_SEPARATOR . $autoconfig['app-root']);
$autoconfig['app-paths'] = array_merge($autoconfig['app-paths'], resolve_app_paths($autoconfig['app-root']));
$root = $autoconfig[Autoconfig::ROOT];
$autoconfig[Autoconfig::APP_ROOT] = realpath($root . DIRECTORY_SEPARATOR . $autoconfig[Autoconfig::APP_ROOT]);
$autoconfig[Autoconfig::APP_PATHS] = array_merge(
$autoconfig[Autoconfig::APP_PATHS],
resolve_app_paths($autoconfig[Autoconfig::APP_ROOT])
);

foreach ($autoconfig['filters'] as $filter)
foreach ($autoconfig[Autoconfig::AUTOCONFIG_FILTERS] as $filter)
{
call_user_func_array($filter, [ &$autoconfig ]);
}
Expand Down
30 changes: 30 additions & 0 deletions lib/Autoconfig/Autoconfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\Autoconfig;

interface Autoconfig
{
const APP_ROOT = 'app-root';
const APP_PATHS = 'app-paths';
const AUTOCONFIG_FILTERS = 'autoconfig-filters';
const CONFIG_CONSTRUCTOR = 'config-constructor';
const CONFIG_PATH = 'config-path';
const LOCALE_PATH = 'locale-path';
const MODULE_PATH = 'module-path';
const CONFIG_WEIGHT = 'config-weight';
const CONFIG_WEIGHT_FRAMEWORK = -100;
const CONFIG_WEIGHT_MODULE = 0;
const CONFIG_WEIGHT_APP = 100;
const ROOT = 'root';

const DEFAULT_APP_ROOT = 'app';
}
70 changes: 34 additions & 36 deletions lib/Autoconfig/AutoconfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
/**
* @codeCoverageIgnore
*/
class AutoconfigGenerator
class AutoconfigGenerator implements Autoconfig
{
const CONFIG_WEIGHT_FRAMEWORK = -100;
const CONFIG_WEIGHT_MODULE = 0;
const CONFIG_WEIGHT_APP = 100;

/**
* @var Package[]
*/
Expand Down Expand Up @@ -129,9 +125,9 @@ protected function resolve_fragments(array $packages)
*/
private function resolve_config_weight(Package $package, array $fragment)
{
if (isset($fragment['config-weight']))
if (isset($fragment[self::CONFIG_WEIGHT]))
{
return $fragment['config-weight'];
return $fragment[self::CONFIG_WEIGHT];
}

if ($package instanceof RootPackage)
Expand Down Expand Up @@ -204,13 +200,13 @@ public function synthesize(Filesystem $filesystem = null)

$config = [

'config-constructor' => [],
'config-path' => [],
'locale-path' => [],
'module-path' => [],
'autoconfig-filters' => [],
'app-root' => 'app',
'app-paths' => []
self::CONFIG_CONSTRUCTOR => [],
self::CONFIG_PATH => [],
self::LOCALE_PATH => [],
self::MODULE_PATH => [],
self::AUTOCONFIG_FILTERS => [],
self::APP_ROOT => self::DEFAULT_APP_ROOT,
self::APP_PATHS => []

];

Expand All @@ -220,15 +216,15 @@ public function synthesize(Filesystem $filesystem = null)
{
switch ($key)
{
case 'config-constructor':
case 'autoconfig-filters':
case 'app-paths':
case self::CONFIG_CONSTRUCTOR:
case self::AUTOCONFIG_FILTERS:
case self::APP_PATHS:

$config[$key] = array_merge($config[$key], (array) $value);

break;

case 'config-path':
case self::CONFIG_PATH:

foreach ((array) $value as $v)
{
Expand All @@ -242,8 +238,8 @@ public function synthesize(Filesystem $filesystem = null)

break;

case 'locale-path':
case 'module-path':
case self::LOCALE_PATH:
case self::MODULE_PATH:

foreach ((array) $value as $v)
{
Expand All @@ -253,7 +249,7 @@ public function synthesize(Filesystem $filesystem = null)
break;


case 'app-root':
case self::APP_ROOT:

$config[$key] = $value;

Expand Down Expand Up @@ -281,56 +277,58 @@ public function render($synthesized_config =null)

$class = __CLASS__;

$config_constructor = $this->render_config_constructor($synthesized_config['config-constructor']);
$config_path = $this->render_config_path($synthesized_config['config-path']);
$locale_path = implode(",\n\t\t", $synthesized_config['locale-path']);
$module_path = implode(",\n\t\t", $synthesized_config['module-path']);
$filters = $this->render_filters($synthesized_config['autoconfig-filters']);
$app_root = $synthesized_config['app-root'];
$app_paths = implode(",\n\t\t", $synthesized_config['app-paths']);
$config_constructor = $this->render_config_constructor($synthesized_config[self::CONFIG_CONSTRUCTOR]);
$config_path = $this->render_config_path($synthesized_config[self::CONFIG_PATH]);
$locale_path = implode(",\n\t\t", $synthesized_config[self::LOCALE_PATH]);
$module_path = implode(",\n\t\t", $synthesized_config[self::MODULE_PATH]);
$filters = $this->render_filters($synthesized_config[self::AUTOCONFIG_FILTERS]);
$app_root = $synthesized_config[self::APP_ROOT];
$app_paths = implode(",\n\t\t", $synthesized_config[self::APP_PATHS]);

return <<<EOT
<?php
// autoconfig.php @generated by $class
use ICanBoogie\Autoconfig\Autoconfig;
return [
'config-constructor' => [
Autoconfig::CONFIG_CONSTRUCTOR => [
$config_constructor
],
'config-path' => [
Autoconfig::CONFIG_PATH => [
$config_path
],
'locale-path' => [
Autoconfig::LOCALE_PATH => [
$locale_path
],
'module-path' => [
Autoconfig::MODULE_PATH => [
$module_path
],
'filters' => [
Autoconfig::AUTOCONFIG_FILTERS => [
$filters
],
'root' => getcwd(),
Autoconfig::ROOT => getcwd(),
'app-root' => "$app_root",
Autoconfig::APP_ROOT => "$app_root",
'app-paths' => [
Autoconfig::APP_PATHS => [
$app_paths
Expand Down
6 changes: 3 additions & 3 deletions lib/Autoconfig/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ static public function on_autoload_dump(Event $event)
}

/**
* Adds the "config" directories found in the app paths to `config-path`.
* Adds the "config" directories found in the app paths to `CONFIG_PATH`.
*
* @param array $autoconfig
*/
static public function filter_autoconfig(array &$autoconfig)
{
foreach ($autoconfig['app-paths'] as $directory)
foreach ($autoconfig[Autoconfig::APP_PATHS] as $directory)
{
if (file_exists($directory . 'config'))
{
$autoconfig['config-path'][$directory . 'config'] = 20;
$autoconfig[Autoconfig::CONFIG_PATH][$directory . 'config'] = Autoconfig::CONFIG_WEIGHT_APP;
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ICanBoogie;

use ICanBoogie\Autoconfig\Autoconfig;
use ICanBoogie\HTTP\Request;
use ICanBoogie\HTTP\Response;
use ICanBoogie\HTTP\Status;
Expand Down Expand Up @@ -142,7 +143,7 @@ public function __construct(array $options = [])
}

$this->bind_object_class();
$this->configs = $this->create_config_manager($options['config-path'], $options['config-constructor']);
$this->configs = $this->create_config_manager($options[Autoconfig::CONFIG_PATH], $options[Autoconfig::CONFIG_CONSTRUCTOR]);
$this->apply_config($this->config);

self::$status = self::STATUS_INSTANTIATED;
Expand Down Expand Up @@ -219,21 +220,21 @@ protected function create_config_manager(array $paths, array $synthesizers)
*/
protected function apply_config(array $config)
{
$error_handler = $config['error_handler'];
$error_handler = $config[AppConfig::ERROR_HANDLER];

if ($error_handler)
{
set_error_handler($error_handler);
}

$exception_handler = $config['exception_handler'];
$exception_handler = $config[AppConfig::EXCEPTION_HANDLER];

if ($exception_handler)
{
set_exception_handler($exception_handler);
}

if ($config['cache configs'])
if ($config[AppConfig::CACHE_CONFIGS])
{
$this->configs->cache = $this->storage_for_configs;
}
Expand Down Expand Up @@ -281,7 +282,7 @@ protected function get_storage_for_configs()
static $storage;

return $storage
?: $storage = $this->create_storage_for_configs($this->config['storage_for_configs']);
?: $storage = $this->create_storage_for_configs($this->config[AppConfig::STORAGE_FOR_CONFIGS]);
}

/**
Expand All @@ -303,7 +304,7 @@ protected function create_storage_for_vars($engine)
*/
protected function lazy_get_vars()
{
return $this->create_storage_for_vars($this->config['storage_for_vars']);
return $this->create_storage_for_vars($this->config[AppConfig::STORAGE_FOR_VARS]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/SessionWithEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SessionWithEvent extends Session
*/
static public function for_app(Application $app)
{
return self::$instance ?: self::$instance = new static($app->config['session']);
return self::$instance ?: self::$instance = new static($app->config[AppConfig::SESSION]);
}

/**
Expand Down

0 comments on commit 267e8b4

Please sign in to comment.