Skip to content

Commit

Permalink
Improved entire Bootstrap logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaposa committed Feb 26, 2017
1 parent c2e0fc2 commit bf3fa96
Show file tree
Hide file tree
Showing 22 changed files with 296 additions and 421 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Besides parameters for supporting services, for example database connection, cac

Ultimately, goal is to load/aggregate configurations based on the environment, and make it available for the application typically in form of an array or an ArrayObject.

While Phoundation is quite flexible in this regard thanks to `Phoundation\Config\Loader\LoaderInterface`, there's a trend adopted by many frameworks and projects for environment-specific configurations, a concept that is nicely described in [Zend Framework's documentation](https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-application-configuration).
While Phoundation is quite flexible in this regard thanks to `Phoundation\Config\Loader\ConfigLoaderInterface`, there's a trend adopted by many frameworks and projects for environment-specific configurations, a concept that is nicely described in [Zend Framework's documentation](https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-application-configuration).

### Dependency Injection

Expand All @@ -59,17 +59,13 @@ Component that connects these different pieces together is `Bootstrap` which is

```php
use Phoundation\Bootstrap\Bootstrap;
use Phoundation\Config\Loader\FileLoader;
use Phoundation\Di\Container\Factory\ZendServiceManagerFactory;

$bootstrap = new Bootstrap([
'config' => [
'foo' => 'bar',
],
'config_glob_paths' => [
sprintf('config/{{,*.}global,{,*.}%s}.php', getenv('APP_ENV') ?: 'local'),
],
'di_container_factory' => ZendServiceManagerFactory::class,
]);
$bootstrap = new Bootstrap(
new FileLoader(glob(sprintf('config/{{,*.}global,{,*.}%s}.php', getenv('APP_ENV') ?: 'local'), GLOB_BRACE)),
new ZendServiceManagerFactory()
);
$diContainer = $bootstrap();

//...
Expand Down
38 changes: 18 additions & 20 deletions src/Bootstrap/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

namespace Phoundation\Bootstrap;

use Phoundation\Config\Loader\ConfigLoaderInterface;
use Phoundation\Di\Container\Factory\DiContainerFactoryInterface;
use Phoundation\Config\Config;
use Psr\Container\ContainerInterface;
use Phoundation\Config\ConfigFactory;
use Phoundation\ErrorHandling\RunnerInterface;

/**
Expand All @@ -23,9 +24,14 @@
class Bootstrap
{
/**
* @var Options
* @var ConfigLoaderInterface
*/
protected $options;
protected $configLoader;

/**
* @var DiContainerFactoryInterface
*/
protected $diContainerFactory;

/**
* @var Config
Expand All @@ -37,14 +43,15 @@ class Bootstrap
*/
protected $diContainer;

public function __construct(array $options)
public function __construct(ConfigLoaderInterface $configLoader, DiContainerFactoryInterface $diContainerFactory)
{
$this->options = Options::fromArray($options);
$this->configLoader = $configLoader;
$this->diContainerFactory = $diContainerFactory;
}

public function __invoke()
{
$this->buildConfig();
$this->loadConfig();
$this->buildDiContainer();

$this->setPhpSettings();
Expand All @@ -53,26 +60,17 @@ public function __invoke()
return $this->diContainer;
}

protected function buildConfig()
protected function loadConfig()
{
$config = Config::fromArray($this->options->getConfig());

foreach ($this->options->getConfigPaths() as $path) {
$config->merge(ConfigFactory::createFromPath($path));
}

foreach ($this->options->getConfigGlobPaths() as $globPath) {
$config->merge(ConfigFactory::createFromGlobPath($globPath));
}

$this->config = $config;
$configLoader = $this->configLoader;
$this->config = $configLoader();
}

protected function buildDiContainer()
{
$diContainerFactory = $this->options->getDiContainerFactory();
$diContainerFactory = $this->diContainerFactory;

$this->diContainer = $diContainerFactory($this->config, $this->options->getDiConfigKey(), $this->options->getConfigServiceName());
$this->diContainer = $diContainerFactory($this->config);
}

protected function setPhpSettings()
Expand Down
125 changes: 0 additions & 125 deletions src/Bootstrap/Options.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,4 @@ public static function fromArray(array $config)
{
return new self($config);
}

public function merge(Config $config)
{
$mergedConfig = self::arrayMerge($this->getArrayCopy(), $config->getArrayCopy());

$this->exchangeArray($mergedConfig);
}

final protected static function arrayMerge(array $a, array $b) : array
{
foreach ($b as $key => $value) {
if (array_key_exists($key, $a)) {
if (is_int($key)) {
$a[] = $value;
} elseif (is_array($value) && is_array($a[$key])) {
$a[$key] = self::arrayMerge($a[$key], $value);
} else {
$a[$key] = $value;
}
} else {
$a[$key] = $value;
}
}

return $a;
}
}
35 changes: 0 additions & 35 deletions src/Config/ConfigFactory.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Config/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of the Phoundation package.
*
* Copyright (c) Nikola Posa
*
* For full copyright and license information, please refer to the LICENSE file,
* located at the package root folder.
*/

declare(strict_types=1);

namespace Phoundation\Config\Loader;

/**
* @author Nikola Posa <[email protected]>
*/
abstract class AbstractLoader implements ConfigLoaderInterface
{
final protected static function arrayMerge(array $a, array $b) : array
{
foreach ($b as $key => $value) {
if (array_key_exists($key, $a)) {
if (is_int($key)) {
$a[] = $value;
} elseif (is_array($value) && is_array($a[$key])) {
$a[$key] = self::arrayMerge($a[$key], $value);
} else {
$a[$key] = $value;
}
} else {
$a[$key] = $value;
}
}

return $a;
}
}
20 changes: 20 additions & 0 deletions src/Config/Loader/CachingLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This file is part of the Phoundation package.
*
* Copyright (c) Nikola Posa
*
* For full copyright and license information, please refer to the LICENSE file,
* located at the package root folder.
*/

declare(strict_types=1);

namespace Phoundation\Config\Loader;

/**
* @author Nikola Posa <[email protected]>
*/
class CachingLoader
{
}
23 changes: 23 additions & 0 deletions src/Config/Loader/ConfigLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* This file is part of the Phoundation package.
*
* Copyright (c) Nikola Posa
*
* For full copyright and license information, please refer to the LICENSE file,
* located at the package root folder.
*/

declare(strict_types=1);

namespace Phoundation\Config\Loader;

use Phoundation\Config\Config;

/**
* @author Nikola Posa <[email protected]>
*/
interface ConfigLoaderInterface
{
public function __invoke() : Config;
}
Loading

0 comments on commit bf3fa96

Please sign in to comment.