diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Config/Loader.php | 150 | ||||
| -rw-r--r-- | src/Filesystem/CreateDirectory.php | 21 | ||||
| -rw-r--r-- | src/Script/Command/Site/Create.php | 42 | 
3 files changed, 194 insertions, 19 deletions
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 @@ +<?php + +namespace App\Config; + +/** + * A class to load a config into an array + * Throws an exception if it fails to laod + * + * @author Phil Burton <phil@pgburton.com> + */ +class Loader +{ +    /** +     * A loaded config array +     * +     * @var mixed[] +     */ +    protected $config; + +    /** +     * Try and load a config +     * +     * @author Phil Burton <phil@pgburton.com> +     */ +    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 <phil@pgburton.com> +     * @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 <phil@pgburton.com> +     * @return string; +     */ +    public function getConfigFilename() +    { +        return 'dipper.php'; +    } + +    /** +     * Make sure the config is up to scratch +     * +     * @author Phil Burton <phil@pgburton.com> +     */ +    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 <phil@pgburton.com> +     */ +    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 <phil@pgburton.com> +     * @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 <phil@pgburton.com> +     * @return mixed[] +     */ +    public function getConfig() +    { +        return $this->config; +    } + +    /** +     * Return the required config keys +     * +     * @author Phil Burton <phil@pgburton.com> +     * @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 <phil@d3r.com>       */ -    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 <phil@pgburton.com> +     * @param string $path +     * @return string +     */ +    protected function getFullSitesPath($path) +    { +        return SITES_ROOT . "/" . $path; +    } + +    /** +     * Run a task +     * +     * @author Phil Burton <phil@pgburton.com> +     * @param mixed $task +     * @param string $message +     * @return string +     */ +    protected function runTask($task, $message)      {          $this->progressBar->advance();          $this->progressBar->setMessage($message);  | 
