summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2017-08-25 01:47:18 +0100
committerFbenas <philbeansburton@gmail.com>2017-08-25 01:47:18 +0100
commit903b671e7d6cc1c289be5dc7f356a9e5fb98dbf7 (patch)
tree005a8c4d9750fc4fe42667c166d000e20ecf21d2
parentd082907cb71f568da01e1f22a2c4eab1e3ced7aa (diff)
Add config and config loader and remove requirement of trailing slashes in config values
-rw-r--r--.gitignore2
-rw-r--r--config/dipper.php.example9
-rw-r--r--scripts/run.php14
-rw-r--r--src/Config/Loader.php150
-rw-r--r--src/Filesystem/CreateDirectory.php21
-rw-r--r--src/Script/Command/Site/Create.php42
6 files changed, 217 insertions, 21 deletions
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 @@
+<?php
+
+ // This is how you configure dipper for your system
+ // Each directory below must exists
+ // Each directory much end with a slash
+ return [
+ "CONFIG_ROOT" => "/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 @@
+<?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);