From 903b671e7d6cc1c289be5dc7f356a9e5fb98dbf7 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Fri, 25 Aug 2017 01:47:18 +0100 Subject: Add config and config loader and remove requirement of trailing slashes in config values --- .gitignore | 2 + config/dipper.php.example | 9 +++ scripts/run.php | 14 +++- src/Config/Loader.php | 150 +++++++++++++++++++++++++++++++++++++ src/Filesystem/CreateDirectory.php | 21 ++++-- src/Script/Command/Site/Create.php | 42 ++++++++--- 6 files changed, 217 insertions(+), 21 deletions(-) create mode 100644 config/dipper.php.example create mode 100644 src/Config/Loader.php diff --git a/.gitignore b/.gitignore index 48b8bf9..f921a3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ vendor/ +config/dipper.php +dipper.php diff --git a/config/dipper.php.example b/config/dipper.php.example new file mode 100644 index 0000000..d43ea42 --- /dev/null +++ b/config/dipper.php.example @@ -0,0 +1,9 @@ + "/path/to/directory", // The directory where we write the nginx.conf partial + "SITES_ROOT" => "/path/to/directory", // The directory where we write the website file and directory structure + ]; diff --git a/scripts/run.php b/scripts/run.php index 0ba999a..2995f8a 100644 --- a/scripts/run.php +++ b/scripts/run.php @@ -7,10 +7,20 @@ error_reporting(E_ALL); // Pull in bootstrap require(realpath(dirname(__FILE__) . "/../bootstrap.php")); + +// Load a config +try { + $loader = new App\Config\Loader(); + $config = $loader->getConfig(); +} catch (Exception $e) { + echo $e->getMessage() . "\n"; + exit; +} + // Set up siteroot // TODO Don't rely on trailing slashes here define('DIPPER_ROOT', realpath(dirname(__FILE__) . "/../") . '/'); -define('SITES_ROOT', '/home/phil/sites/'); -define('CONFIG_ROOT', '/home/phil/sites/'); +define('SITES_ROOT', $config['SITES_ROOT']); +define('CONFIG_ROOT', $config['SITES_ROOT']); new App\Script\Console; diff --git a/src/Config/Loader.php b/src/Config/Loader.php new file mode 100644 index 0000000..97df62e --- /dev/null +++ b/src/Config/Loader.php @@ -0,0 +1,150 @@ + + */ +class Loader +{ + /** + * A loaded config array + * + * @var mixed[] + */ + protected $config; + + /** + * Try and load a config + * + * @author Phil Burton + */ + public function __construct() + { + foreach ($this->getConfigPaths() as $path) { + if ($config = $this->loadConfig($path)) { + $this->validate(); + $this->clean(); + return; + } + } + + // No config found + throw new \Exception('Could not find config file.'); + } + + /** + * Try and load a config file + * If we can't return false + * + * @author Phil Burton + * @return mixed[]|false + */ + public function loadConfig(string $path) + { + $filename = $path . '/' . $this->getConfigFilename(); + + if (!$this->config) { + if (!file_exists($filename)) { + return false; + } + + if (!is_readable($filename)) { + return false; + } + + if (!$config = include($filename)) { + return false; + } + + $this->config = $config; + } + + return $this->config; + } + + /** + * Return the nsam of the config file to load + * + * @author Phil Burton + * @return string; + */ + public function getConfigFilename() + { + return 'dipper.php'; + } + + /** + * Make sure the config is up to scratch + * + * @author Phil Burton + */ + public function validate() + { + $config = $this->getConfig(); + + foreach ($this->getRequired() as $required) { + if (!isset($config[$required])) { + throw new \Exception("Could not find " . $required . " in config file."); + } + } + } + + /** + * Clean up the config ready for use + * + * @author Phil Burton + */ + public function clean() + { + $config = $this->getConfig(); + foreach ($config as &$item) { + // Remove trailing slashes + $item = rtrim($item, "/"); + } + + $this->config = $config; + } + + /** + * Return the array of config paths + * + * @author Phil Burton + * @return string[] + */ + public function getConfigPaths() + { + return [ + realpath(dirname(__FILE__) . "/../../"), // Root of dipper + realpath(dirname(__FILE__) . "/../../config") // Config directory in dipper + ]; + } + + /** + * Return the loaded config + * + * @author Phil Burton + * @return mixed[] + */ + public function getConfig() + { + return $this->config; + } + + /** + * Return the required config keys + * + * @author Phil Burton + * @return string[] + */ + public function getRequired() + { + return [ + "SITES_ROOT", + "CONFIG_ROOT" + ]; + } +} diff --git a/src/Filesystem/CreateDirectory.php b/src/Filesystem/CreateDirectory.php index c74cba4..64c5fba 100644 --- a/src/Filesystem/CreateDirectory.php +++ b/src/Filesystem/CreateDirectory.php @@ -16,7 +16,12 @@ class CreateDirectory */ protected $thing = "directory"; - protected $filename; + /** + * Directory to create + * + * @var string + */ + protected $directory; /** * Check if the directory already exists @@ -24,16 +29,16 @@ class CreateDirectory * @param string $filename * @author Phil Burton */ - public function __construct(string $filename) + public function __construct(string $directory) { - $this->filename = $filename; + $this->directory = $directory; - if (!is_writable(dirname($filename))) { - throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $filename); + if (!is_writable(dirname($directory))) { + throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $directory); } - if (file_exists($filename)) { - throw new \Exception('Directory already exists at: ' . $filename); + if (file_exists($directory)) { + throw new \Exception('Directory already exists at: ' . $directory); } } @@ -44,6 +49,6 @@ class CreateDirectory */ public function execute() { - mkdir($this->filename); + mkdir($this->directory); } } diff --git a/src/Script/Command/Site/Create.php b/src/Script/Command/Site/Create.php index 81df87e..e300ef7 100644 --- a/src/Script/Command/Site/Create.php +++ b/src/Script/Command/Site/Create.php @@ -58,10 +58,10 @@ class Create extends SyCommand $domain = $input->getArgument('domain'); $output->writeln( - 'Creating new site with domain: ' - . $input->getArgument('domain') - . ' and name: ' + 'Creating new site with name: ' . $input->getArgument('name') + . ' and domain: ' + . $input->getArgument('domain') ); // Set-up progress bar @@ -79,12 +79,12 @@ class Create extends SyCommand try { $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory( - SITES_ROOT . $name + $this->getFullSitesPath($name) ); $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile( CONFIG_ROOT . $name . '.conf', - include_once(DIPPER_ROOT . 'nginx/stub.php') + include_once(DIPPER_ROOT . '/nginx/stub.php') ); } catch (\Exception $e) { $output->writeln(''); @@ -104,35 +104,35 @@ class Create extends SyCommand $this->runTask( new \App\Filesystem\CreateDirectory( - SITES_ROOT . $name . '/logs' + $this->getFullSitesPath($name) . '/logs' ), 'Creating logs directory' ); $this->runTask( new \App\Filesystem\CreateFile( - SITES_ROOT . $name . '/logs/' . $name . '.access_log' + $this->getFullSitesPath($name) . '/logs/' . $name . '.access_log' ), 'Creating access log file' ); $this->runTask( new \App\Filesystem\CreateFile( - SITES_ROOT . $name . '/logs/' . $name . '.errors_log' + $this->getFullSitesPath($name) . '/logs/' . $name . '.errors_log' ), 'Creating error log file' ); $this->runTask( new \App\Filesystem\CreateDirectory( - SITES_ROOT . $name . '/web' + $this->getFullSitesPath($name) . '/web' ), 'Creating web folder' ); $this->runTask( new \App\Filesystem\CreateFile( - SITES_ROOT . $name . '/web/index.php' + $this->getFullSitesPath($name) . '/web/index.php' ), 'Creating directory index.php' ); @@ -159,7 +159,27 @@ class Create extends SyCommand $output->writeln('Complete'); } - public function runTask($task, $message) + /** + * Return the full path of a given file + * + * @author Phil Burton + * @param string $path + * @return string + */ + protected function getFullSitesPath($path) + { + return SITES_ROOT . "/" . $path; + } + + /** + * Run a task + * + * @author Phil Burton + * @param mixed $task + * @param string $message + * @return string + */ + protected function runTask($task, $message) { $this->progressBar->advance(); $this->progressBar->setMessage($message); -- cgit v1.2.3