From edcdbf0e6fbf6accc37d2b36c4e5ecb21cd4d007 Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Mon, 15 Jun 2020 08:19:42 +0200 Subject: [PATCH] Working building. --- src/App.php | 9 +++- src/Services/ConfigurationService.php | 69 +++++++++++++++++++++++++-- src/Services/EnvironmentService.php | 6 +++ src/Services/SessionService.php | 4 +- 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/App.php b/src/App.php index 30fe958..5f24eb2 100644 --- a/src/App.php +++ b/src/App.php @@ -88,6 +88,7 @@ class App public function setupContainer(): Container { + $app = $this; $container = (new ContainerBuilder()) ->useAutowiring(true) @@ -136,9 +137,13 @@ class App return new EnvironmentService(); }); - $container->set(ConfigurationService::class, function(ContainerInterface $container){ - return new ConfigurationService($container->get(EnvironmentService::class)); + $container->set(ConfigurationService::class, function(ContainerInterface $container) use ($app){ + return new ConfigurationService( + $app, + $container->get(EnvironmentService::class) + ); }); + $container->set(\Faker\Generator::class, function(ContainerInterface $c) { $faker = FakerFactory::create(); $faker->addProvider(new Provider\Base($faker)); diff --git a/src/Services/ConfigurationService.php b/src/Services/ConfigurationService.php index fb36e7c..c9bee8d 100644 --- a/src/Services/ConfigurationService.php +++ b/src/Services/ConfigurationService.php @@ -2,13 +2,14 @@ namespace Benzine\Services; +use Benzine\App; +use Benzine\ORM\Connection\Database; use Symfony\Component\Yaml\Yaml; class ConfigurationService { public const KEY_APP_NAME = 'application/name'; public const KEY_APP_ROOT = 'application/root'; - public const KEY_CLASS = 'application/class'; public const KEY_DEBUG_ENABLE = 'application/debug'; public const KEY_DEFAULT_ACCESS = 'application/default_access'; public const KEY_TIMEZONE = 'application/timezone'; @@ -17,18 +18,21 @@ class ConfigurationService public const KEY_LOG_FORMAT_DATE = 'logging/format_date'; public const KEY_LOG_FORMAT_MESSAGE = 'logging/format_message'; + protected App $app; protected EnvironmentService $environmentService; protected string $appRoot; protected array $config; - public function __construct(EnvironmentService $environmentService) + public function __construct(App $app, EnvironmentService $environmentService) { + $this->app = $app; $this->environmentService = $environmentService; $this->findConfig(); $this->setupDefines(); } - protected function setupDefines() : void { + protected function setupDefines() : void + { define("APP_ROOT", $this->appRoot); define("APP_NAME", $this->get('application/name')); } @@ -49,12 +53,31 @@ class ConfigurationService return $this->findConfig($parentPath); } - $this->config = Yaml::parseFile($path . "/.benzine.yml"); + $this->parseFile($path . "/.benzine.yml"); $this->appRoot = $path; return true; } + protected function parseFile(string $file) + { + $yaml = file_get_contents($file); + foreach($this->environmentService->all() as $key => $value){ + if(is_string($value)) { + $yaml = str_replace("\$$key", $value, $yaml); + } + } + $this->config = Yaml::parse($yaml); + } + + public function has(string $key) : bool { + return $this->get($key) !== null; + } + /** + * @param string $key + * @param string|null $defaultValue + * @return string|array|null + */ public function get(string $key, string $defaultValue = null){ $scope = $this->config; foreach (explode('/', strtolower($key)) as $keyBit) { @@ -62,7 +85,7 @@ class ConfigurationService } if (is_array($scope)) { - return trim(end($scope)); + return $scope; } if (!$scope) { @@ -71,4 +94,40 @@ class ConfigurationService return trim($scope); } + + /** + * @return Database[] + */ + public function getDatabases() : array + { + $databases = []; + foreach($this->config['databases'] as $name => $config){ + $database = new Database($name, $config); + $databases[$database->getName()] = $database; + } + return $databases; + } + + public function getNamespace(): string + { + $coreClass = explode("\\", $this->getCore()); + array_pop($coreClass); + return implode("\\", $coreClass); + } + + public function getCore(): string + { + return $this->get('application/core'); + } + + public function getAppName(): string + { + return $this->get('application/name'); + } + + public function getLaminatorTemplates(): array + { + return $this->get('laminator/templates') + ?? ['Models', 'Services', 'Controllers', 'Endpoints', 'Routes']; + } } \ No newline at end of file diff --git a/src/Services/EnvironmentService.php b/src/Services/EnvironmentService.php index bc72632..56fc512 100644 --- a/src/Services/EnvironmentService.php +++ b/src/Services/EnvironmentService.php @@ -16,6 +16,12 @@ class EnvironmentService return $this->get($key) !== null; } + public function all() : array + { + ksort($this->environmentVariables); + return $this->environmentVariables; + } + public function get(string $key, string $default = null){ if(isset($this->environmentVariables[$key])){ return $this->environmentVariables[$key]; diff --git a/src/Services/SessionService.php b/src/Services/SessionService.php index 392cb5f..f99a63d 100644 --- a/src/Services/SessionService.php +++ b/src/Services/SessionService.php @@ -4,7 +4,9 @@ namespace Benzine\Services; class SessionService { - public function __construct() + protected \Redis $redis; + public function __construct(\Redis $redis) { + $this->redis = $redis; } } \ No newline at end of file