Working building.

This commit is contained in:
Greyscale 2020-06-15 08:19:42 +02:00
parent be507e0d4c
commit edcdbf0e6f
4 changed files with 80 additions and 8 deletions

View file

@ -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));

View file

@ -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'];
}
}

View file

@ -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];

View file

@ -4,7 +4,9 @@ namespace Benzine\Services;
class SessionService
{
public function __construct()
protected \Redis $redis;
public function __construct(\Redis $redis)
{
$this->redis = $redis;
}
}