app = $app; $this->environmentService = $environmentService; $this->findConfig(); $this->setupDefines(); } public function has(string $key): bool { return null !== $this->get($key); } /** * @param string $key * @param null|string $defaultValue * * @return null|array|string */ public function get(string $key, string $defaultValue = null) { $scope = $this->config; foreach (explode('/', strtolower($key)) as $keyBit) { $scope = &$scope[$keyBit]; } if (is_array($scope)) { return $scope; } if (!$scope) { return $defaultValue; } return trim($scope); } public function getNamespace(): string { $coreClass = explode('\\', $this->getCore()); array_pop($coreClass); $namespace = implode('\\', $coreClass); return ltrim($namespace, '\\'); } 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']; } protected function setupDefines(): void { define('APP_ROOT', $this->appRoot); define('APP_NAME', $this->get('application/name')); } /** * Locate .benzine.yml. * * @param null|string $path */ protected function findConfig(string $path = null): bool { if (!$path) { $path = getcwd(); //$path = dirname($this->environmentService->get('SCRIPT_FILENAME')); } if (!file_exists($path.'/.benzine.yml')) { $currentDirElem = explode(DIRECTORY_SEPARATOR, $path); array_pop($currentDirElem); $parentPath = implode(DIRECTORY_SEPARATOR, $currentDirElem); return $this->findConfig($parentPath); } $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); } }