summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2017-08-22 00:45:29 +0100
committerFbenas <philbeansburton@gmail.com>2017-08-22 00:45:29 +0100
commit655c4f38241ab58a3404acb15a751f07dc1bca6a (patch)
tree7ae2cca93857025e4d83bf67600d47b4a5f20058
parent31ac8bccdae86eeb150cb396c44ee88427120e3e (diff)
Complete refactor
-rw-r--r--scripts/test.php4
-rw-r--r--src/Filesystem/Create.php45
-rw-r--r--src/Filesystem/CreateDirectory.php16
-rw-r--r--src/Filesystem/CreateFile.php17
-rw-r--r--src/Script/Command/Site/Create.php119
-rw-r--r--src/Script/Console.php12
6 files changed, 126 insertions, 87 deletions
diff --git a/scripts/test.php b/scripts/test.php
index d14076a..f1203ce 100644
--- a/scripts/test.php
+++ b/scripts/test.php
@@ -9,7 +9,7 @@ require(realpath(dirname(__FILE__) . "/../bootstrap.php"));
// Set up siteroot
define('DIPPER_ROOT', realpath(dirname(__FILE__) . "/../"));
-define('SITES_ROOT', '/web/sites/');
-define('CONFIG_ROOT', '/web/etc/nginx/');
+define('SITES_ROOT', '/home/phil/sites/');
+define('CONFIG_ROOT', '/home/phil/sites/');
new App\Script\Console;
diff --git a/src/Filesystem/Create.php b/src/Filesystem/Create.php
deleted file mode 100644
index 45b09fd..0000000
--- a/src/Filesystem/Create.php
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-
-namespace App\Filesystem;
-
-
-/**
- * Base class for creating filesystem things
- *
- * @author Phil Burton <phil@d3r.com>
- */
-abstract class Create
-{
- /**
- * Filename to create
- *
- * @var string
- * @author Phil Burton <phil@d3r.com>
- */
- protected $filename;
-
- /**
- * Thing we're creating, used for messages
- *
- * @var string
- * @author Phil Burton <phil@d3r.com>
- */
- protected $thing;
-
- /**
- * Set filename on object and check we can wirte to the Directory
- *
- * @param string $filename
- * @author Phil Burton <phil@d3r.com>
- */
- public function __construct(string $filename)
- {
- $this->filename = $filename;
-
- if (!is_writable(dirname($filename))) {
- throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $filename);
- }
- }
-
- abstract public function excecute();
-}
diff --git a/src/Filesystem/CreateDirectory.php b/src/Filesystem/CreateDirectory.php
index 2af53d7..c74cba4 100644
--- a/src/Filesystem/CreateDirectory.php
+++ b/src/Filesystem/CreateDirectory.php
@@ -2,14 +2,12 @@
namespace App\Filesystem;
-use App\Filesystem\Create;
-
/**
* Class to create a new directory
*
* @author Phil Burton <phil@d3r.com>
*/
-class CreateDirectory extends Create
+class CreateDirectory
{
/**
* Thing we're creating
@@ -18,6 +16,8 @@ class CreateDirectory extends Create
*/
protected $thing = "directory";
+ protected $filename;
+
/**
* Check if the directory already exists
*
@@ -26,7 +26,11 @@ class CreateDirectory extends Create
*/
public function __construct(string $filename)
{
- parent::__construct($filename);
+ $this->filename = $filename;
+
+ if (!is_writable(dirname($filename))) {
+ throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $filename);
+ }
if (file_exists($filename)) {
throw new \Exception('Directory already exists at: ' . $filename);
@@ -38,8 +42,8 @@ class CreateDirectory extends Create
*
* @author Phil Burton <phil@d3r.com>
*/
- public function excecute()
+ public function execute()
{
- echo "execute";
+ mkdir($this->filename);
}
}
diff --git a/src/Filesystem/CreateFile.php b/src/Filesystem/CreateFile.php
index 7fed65c..08ba0db 100644
--- a/src/Filesystem/CreateFile.php
+++ b/src/Filesystem/CreateFile.php
@@ -2,15 +2,12 @@
namespace App\Filesystem;
-use App\Filesystem\Create;
-
/**
* Class to create a new file
*
- * @param string $filename [description]
* @author Phil Burton <phil@d3r.com>
*/
-class CreateFile extends Create
+class CreateFile
{
/**
* Thing we're creating
@@ -19,6 +16,8 @@ class CreateFile extends Create
*/
protected $thing = "file";
+ protected $filename;
+
/**
* Check if the file already exists
*
@@ -27,7 +26,11 @@ class CreateFile extends Create
*/
public function __construct(string $filename)
{
- parent::__construct($filename);
+ $this->filename = $filename;
+
+ if (!is_writable(dirname($filename))) {
+ throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $filename);
+ }
if (file_exists($filename)) {
throw new \Exception('File already exists at: ' . $filename);
@@ -39,8 +42,8 @@ class CreateFile extends Create
*
* @author Phil Burton <phil@d3r.com>
*/
- public function excecute()
+ public function execute()
{
- echo "execute";
+ touch($this->filename);
}
}
diff --git a/src/Script/Command/Site/Create.php b/src/Script/Command/Site/Create.php
index 2526d2e..c7936fd 100644
--- a/src/Script/Command/Site/Create.php
+++ b/src/Script/Command/Site/Create.php
@@ -1,11 +1,12 @@
<?php
-namespace App\Script\Command;
+namespace App\Script\Command\Site;
use Symfony\Component\Console\Command\Command as SyCommand;
use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use App\Filesystem\CreateFile;
use App\Filesystem\CreateDirectory;
@@ -17,6 +18,8 @@ use App\Filesystem\CreateDirectory;
*/
class Create extends SyCommand
{
+ protected $progressBar;
+
/**
* Configure the command
*
@@ -31,6 +34,13 @@ class Create extends SyCommand
->setName('site:create')
->setDescription('Create a new site.')
->setHelp('This command allows you to create a new site.');
+
+ $this->addOption(
+ 'test',
+ 't',
+ InputOption::VALUE_NONE,
+ 'Run in test mode without any file or directory created?'
+ );
}
/**
@@ -55,45 +65,104 @@ class Create extends SyCommand
);
// Set-up progress bar
- $progressBar = new ProgressBar($output, $taskCount);
- $progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%');
- $progressBar->setFormat('custom');
- $progressBar->setMessage('Starting...');
- $progressBar->start();
- $progressBar->setMessage('Running pre-flight checks');
+ $this->progressBar = new ProgressBar($output, $taskCount);
+ $this->progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%');
+ $this->progressBar->setFormat('custom');
+ $this->progressBar->setMessage('Starting...');
+ $this->progressBar->start();
+ $this->progressBar->setMessage('Running pre-flight checks');
// Construct the file commands so we run checks first
- $progressBar->advance();
+ $this->progressBar->advance();
$tasks = [];
try {
- $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory(SITES_ROOT . $name);
- $tasks['Creating logs directory'] = new \App\Filesystem\CreateDirectory(SITES_ROOT . $name . '/logs');
- $tasks['Creating access log file'] = new \App\Filesystem\CreateFile(SITES_ROOT . $name . '/logs/' . $name . '.access_log');
- $tasks['Creating error log file'] = new \App\Filesystem\CreateFile(SITES_ROOT . $name . '/logs/' . $name . '.errors_log');
- $tasks['Creating web folder'] = new \App\Filesystem\CreateDirectory(SITES_ROOT . $name . '/web');
- $tasks['Creating directory index.php'] = new \App\Filesystem\CreateFile(SITES_ROOT . $name . '/web/index.php');
- $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile(CONFIG_ROOT . $name . '.conf');
+ $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory(
+ SITES_ROOT . $name
+ );
+
+ $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile(
+ CONFIG_ROOT . $name . '.conf'
+ );
} catch (\Exception $e) {
+ $output->writeln('');
$output->writeln('Error with pre-flight checks');
$output->writeln($e->getMessage());
exit;
}
- foreach ($tasks as $message => $task) {
- // Create file in web root for execution
- $progressBar->advance();
- $progressBar->setMessage($message);
- $task->execute();
- sleep(1);
- }
+ if (!$input->getOption("test")) {
+ foreach ($tasks as $message => $task) {
+ // Create file in web root for execution
+ $this->runTask($task, $message);
+ }
+
+ try {
+ $tasks = [];
+
+ $this->runTask(
+ new \App\Filesystem\CreateDirectory(
+ SITES_ROOT . $name . '/logs'
+ ),
+ 'Creating logs directory'
+ );
+ $this->runTask(
+ new \App\Filesystem\CreateFile(
+ SITES_ROOT . $name . '/logs/' . $name . '.access_log'
+ ),
+ 'Creating access log file'
+ );
+
+ $this->runTask(
+ new \App\Filesystem\CreateFile(
+ SITES_ROOT . $name . '/logs/' . $name . '.errors_log'
+ ),
+ 'Creating error log file'
+ );
+
+ $this->runTask(
+ new \App\Filesystem\CreateDirectory(
+ SITES_ROOT . $name . '/web'
+ ),
+ 'Creating web folder'
+ );
+
+ $this->runTask(
+ new \App\Filesystem\CreateFile(
+ SITES_ROOT . $name . '/web/index.php'
+ ),
+ 'Creating directory index.php'
+ );
+
+
+ foreach ($tasks as $task) {
+ // Create file in web root for execution
+ $this->progressBar->advance();
+ $this->progressBar->setMessage($task[0]);
+ $task[1]->execute();
+ sleep(1);
+ }
+ } catch (\Exception $e) {
+ $output->writeln('Unexpected Error creating files and directories');
+ $output->writeln($e->getMessage());
+ exit;
+ }
+ }
// Finish up
- $progressBar->advance();
- $progressBar->finish();
+ $this->progressBar->advance();
+ $this->progressBar->finish();
$output->writeln('');
$output->writeln('Complete');
}
+
+ public function runTask($task, $message)
+ {
+ $this->progressBar->advance();
+ $this->progressBar->setMessage($message);
+ $task->execute();
+ sleep(1);
+ }
}
diff --git a/src/Script/Console.php b/src/Script/Console.php
index 926f41f..5f5d90d 100644
--- a/src/Script/Console.php
+++ b/src/Script/Console.php
@@ -3,14 +3,22 @@
namespace App\Script;
use Symfony\Component\Console\Application;
-use App\Script\Command\Site;
+use App\Script\Command\Site\Create;
+/**
+ * Main application class
+ */
class Console
{
+ /**
+ * Create symfony application and add commands
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function __construct()
{
$application = new Application();
- $application->add(new Site());
+ $application->add(new Create());
$application->run();
}
}