diff options
Diffstat (limited to 'source/Command')
24 files changed, 2070 insertions, 0 deletions
diff --git a/source/Command/AbstractCommand.php b/source/Command/AbstractCommand.php new file mode 100644 index 0000000..55b91eb --- /dev/null +++ b/source/Command/AbstractCommand.php @@ -0,0 +1,53 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-13 + */ + +/** + * Class Command_AbstractCommand + */ +abstract class Command_AbstractCommand implements Command_CommandInterface +{ + /** + * @var Input + */ + protected $input; + + /** + * @var Output + */ + protected $output; + + /** + * @return Input + */ + public function getInput() + { + return $this->input; + } + + /** + * @param Input $input + */ + public function setInput(Input $input) + { + $this->input = $input; + } + + /** + * @return null|Output + */ + public function getOutput() + { + return $this->output; + } + + /** + * @param Output $output + */ + public function setOutput(Output $output) + { + $this->output = $output; + } +}
\ No newline at end of file diff --git a/source/Command/Backup.php b/source/Command/Backup.php new file mode 100644 index 0000000..14b74c6 --- /dev/null +++ b/source/Command/Backup.php @@ -0,0 +1,107 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-16 + */ + +/** + * Class Command_Backup + */ +class Command_Backup extends Command_AbstractCommand +{ + /** + * @var Configuration_Path + */ + private $pathConfiguration; + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @param Configuration_Path $configuration + */ + public function setPathConfiguration(Configuration_Path $configuration) + { + $this->pathConfiguration = $configuration; + } + + /** + * @param Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @throws Exception + */ + public function execute() + { + if (!$this->filesystem->isDirectory($this->pathConfiguration->getBackupPath())) { + $this->filesystem->createDirectory($this->pathConfiguration->getBackupPath()); + } + + $identifierToPaths = array( + 'channels' => array( + 'backup' => $this->pathConfiguration->getBackupChannelsFilePath(), + 'chat' => $this->pathConfiguration->getChatChannelsFilePath() + ), + 'application' => array( + 'backup' => $this->pathConfiguration->getBackupConfigurationFilePath(), + 'chat' => $this->pathConfiguration->getChatConfigurationFilePath() + ), + 'users' => array( + 'backup' => $this->pathConfiguration->getBackupUsersFilePath(), + 'chat' => $this->pathConfiguration->getChatUsersFilePath() + ), + 'version' => array( + 'backup' => $this->pathConfiguration->getBackupVersionFilePath(), + 'chat' => $this->pathConfiguration->getChatVersionFilePath() + ), + ); + + foreach ($identifierToPaths as $identifier => $paths) { + if ($this->filesystem->isFile($paths['backup'])) { + $this->output->addLine($identifier . ' backup file available, will delete it ...'); + $this->filesystem->deleteFile($paths['backup']); + } + } + + foreach ($identifierToPaths as $identifier => $paths) { + echo 'creating backup of ' . $identifier . ' ...' . PHP_EOL; + $this->filesystem->copy( + $paths['chat'], + $paths['backup'] + ); + } + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + if (is_null($this->pathConfiguration)) { + throw new Exception( + 'application is mandatory' + ); + } + + if (is_null($this->filesystem)) { + throw new Exception( + 'filesystem is mandatory' + ); + } + } +}
\ No newline at end of file diff --git a/source/Command/Channel.php b/source/Command/Channel.php new file mode 100644 index 0000000..aa5c235 --- /dev/null +++ b/source/Command/Channel.php @@ -0,0 +1,127 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-16 + */ + +class Command_Channel extends Command_AbstractCommand +{ + /** + * @var AbstractApplication + */ + private $application; + + /** + * @var string + */ + private $command; + + /** + * @var array + */ + private $commands = array( + 'add', + 'edit', + 'delete', + 'list' + ); + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @param AbstractApplication $application + */ + public function setApplication(AbstractApplication $application) + { + $this->application = $application; + } + + /** + * @param Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @throws Exception + */ + public function execute() + { + switch ($this->command) { + case 'add': + $command = $this->application->getChannelAddCommand(); + break; + case 'edit': + $command = $this->application->getChannelEditCommand(); + break; + case 'delete': + $command = $this->application->getChannelDeleteCommand(); + break; + case 'list': + $command = $this->application->getChannelListCommand(); + break; + default: + throw new Exception( + 'unsupported command "' . $this->command . '"' + ); + } + + $command->setInput($this->input); + $command->setOutput($this->output); + + try { + $command->verify(); + } catch (Exception $exception) { + throw new Exception( + '--' . $this->command . ' ' . implode("\n", $command->getUsage()) . PHP_EOL . + PHP_EOL . + $exception->getMessage() + ); + } + $command->execute(); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + '[--' . implode('|--', $this->commands) . ']' + ); + } + + /** + * @throws Exception + */ + public function verify() + { + $this->command = null; + + if ($this->input->getNumberOfArguments() < 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + foreach ($this->commands as $command) { + if ($this->input->hasLongOption($command)) { + $this->command = $command; + break; + } + } + + if (is_null($this->command)) { + throw new Exception( + 'invalid command provided' + ); + } + + $this->input->removeLongOption($this->command); + } +}
\ No newline at end of file diff --git a/source/Command/Channel/AbstractCommand.php b/source/Command/Channel/AbstractCommand.php new file mode 100644 index 0000000..42b6836 --- /dev/null +++ b/source/Command/Channel/AbstractCommand.php @@ -0,0 +1,37 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_AbstractCommand + */ +abstract class Command_Channel_AbstractCommand extends Command_AbstractCommand implements Command_Channel_CommandInterface +{ + /** + * @var File + */ + protected $file; + + /** + * @var array + */ + protected $channels; + + /** + * @param array $channels + */ + public function setChannels(array $channels) + { + $this->channels = $channels; + } + + /** + * @param File $file + */ + public function setChannelFile(File $file) + { + $this->file = $file; + } +}
\ No newline at end of file diff --git a/source/Command/Channel/Add.php b/source/Command/Channel/Add.php new file mode 100644 index 0000000..751970c --- /dev/null +++ b/source/Command/Channel/Add.php @@ -0,0 +1,66 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_Add + */ +class Command_Channel_Add extends Command_Channel_AbstractCommand +{ + /** + * @var string + */ + private $inputName; + + /** + * @throws Exception + */ + public function execute() + { + end($this->channels); + $nextKey = (key($this->channels) + 1); + reset($this->channels); + + $content = $this->file->read(); + + $content[] = '// added - ' . date('Y-m-d H:i:s'); + $content[] = '$channels[' . $nextKey . '] = \'' . $this->inputName . '\';'; + + $this->file->write($content); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'name="<name>"', + ' available channels: ' . implode(',', array_keys($this->channels)), + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $name = $this->input->getParameterValue('name'); + + if (is_null($name)) { + throw new Exception( + 'invalid name "' . $name . '" provided' + ); + } + + $this->inputName = $name; + } +} diff --git a/source/Command/Channel/CommandInterface.php b/source/Command/Channel/CommandInterface.php new file mode 100644 index 0000000..72af707 --- /dev/null +++ b/source/Command/Channel/CommandInterface.php @@ -0,0 +1,21 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Interface Command_Channel_CommandInterface + */ +interface Command_Channel_CommandInterface extends Command_CommandInterface +{ + /** + * @param array $channels + */ + public function setChannels(array $channels); + + /** + * @param File $file + */ + public function setChannelFile(File $file); +}
\ No newline at end of file diff --git a/source/Command/Channel/Delete.php b/source/Command/Channel/Delete.php new file mode 100644 index 0000000..af7a791 --- /dev/null +++ b/source/Command/Channel/Delete.php @@ -0,0 +1,87 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_Delete + */ +class Command_Channel_Delete extends Command_Channel_AbstractCommand +{ + /** + * @var int + */ + private $inputId; + + /** + * @throws Exception + */ + public function execute() + { + reset($this->channels); + + $lines = $this->file->read(); + $content = array(); + + foreach ($lines as $line) { + if ($line == '// Sample channel list:') { + $content[] = $line; + break; + } else { + $content[] = $line; + } + } + + if (empty($this->channels)) { + throw new Exception( + 'nothing to delete' + ); + } else { + unset($this->channels[$this->inputId]); + $ids = array_values($this->channels); + + if (!empty($ids)) { + foreach ($ids as $id => $name) { + $content[] = '$channels[' . $id . '] = \'' . $name . '\';'; + } + } + + $this->file->write($content); + } + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'channel_id=<channel id>', + ' available channels: ' . implode(',', array_keys($this->channels)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $validIds = array_keys($this->channels); + $inputId = $this->input->getParameterValue('channel_id'); + + if (!isset($validIds[$inputId])) { + throw new Exception( + 'invalid name "' . $inputId . '" provided' + ); + } + + $this->inputId = $inputId; + } +} diff --git a/source/Command/Channel/Edit.php b/source/Command/Channel/Edit.php new file mode 100644 index 0000000..fc852b6 --- /dev/null +++ b/source/Command/Channel/Edit.php @@ -0,0 +1,102 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_Edit + */ +class Command_Channel_Edit extends Command_Channel_AbstractCommand +{ + /** + * @var string + */ + private $inputName; + + /** + * @var int + */ + private $inputId; + + /** + * @throws Exception + */ + public function execute() + { + reset($this->channels); + + $lines = $this->file->read(); + $content = array(); + $contentAfterCurrentChannel = array(); + $string = new String(); + + $foundCurrentChannelLine = false; + $linePrefixToSearchFor = '$channels[' . $this->inputId . ']'; + + foreach ($lines as $line) { + if ($string->startsWith($line, $linePrefixToSearchFor)) { + $foundCurrentChannelLine = true; + } else { + if ($foundCurrentChannelLine) { + $contentAfterCurrentChannel[] = $line; + } else { + $content[] = $line; + } + } + } + + $content[] = '$channels[' . $this->inputId . '] = \'' . $this->inputName . '\';'; + + foreach ($contentAfterCurrentChannel as $line) { + $content[] = $line; + } + + $this->file->write($content); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'id=<id> name="<name>"', + ' available channels: ' . implode(',', array_keys($this->channels)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 2) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $name = $this->input->getParameterValue('name'); + $id = $this->input->getParameterValue('id'); + + if (is_null($name)) { + throw new Exception( + 'no name provided' + ); + } + + if (is_null($id)) { + throw new Exception( + 'no id provided' + ); + } else if (!isset($this->channels[$id])) { + throw new Exception( + 'invalid id provided' + ); + } + + $this->inputName = $name; + $this->inputId = $id; + } +}
\ No newline at end of file diff --git a/source/Command/Channel/List.php b/source/Command/Channel/List.php new file mode 100644 index 0000000..caaa445 --- /dev/null +++ b/source/Command/Channel/List.php @@ -0,0 +1,54 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_Channel_List + */ +class Command_Channel_List extends Command_Channel_AbstractCommand +{ + /** + * @throws Exception + */ + public function execute() + { + $numberOfChannels = count($this->channels); + + $this->output->addLine('number of channels: ' . $numberOfChannels); + + if ($numberOfChannels > 0) { + $this->output->addLine(); + $this->output->addLine('id | name '); + $this->output->addLine('--------'); + + foreach ($this->channels as $id => $name) { + $this->output->addLine( + implode( + ' | ', + array( + $id, + $name + ) + ) + ); + } + } + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + } +} diff --git a/source/Command/CommandInterface.php b/source/Command/CommandInterface.php new file mode 100644 index 0000000..afd9908 --- /dev/null +++ b/source/Command/CommandInterface.php @@ -0,0 +1,26 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-13 + */ + +/** + * Interface Command_CommandInterface + */ +interface Command_CommandInterface extends InputDependentInterface, OutputDependentInterface +{ + /** + * @throws Exception + */ + public function execute(); + + /** + * @return array + */ + public function getUsage(); + + /** + * @throws Exception + */ + public function verify(); +}
\ No newline at end of file diff --git a/source/Command/Install.php b/source/Command/Install.php new file mode 100644 index 0000000..936ae69 --- /dev/null +++ b/source/Command/Install.php @@ -0,0 +1,96 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-17 + */ + +/** + * Class Command_Install + */ +class Command_Install extends Command_AbstractCommand +{ + /** + * @var Configuration_Path + */ + private $pathConfiguration; + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @param Configuration_Path $configuration + */ + public function setPathConfiguration(Configuration_Path $configuration) + { + $this->pathConfiguration = $configuration; + } + + /** + * @param Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @throws Exception + * @todo implement setup of database (check if connection is available, split it up in multiple steps (Command_Install_Files, Command_Install_Database) + */ + public function execute() + { + $identifierToPaths = array( + 'channels' => array( + 'example' => $this->pathConfiguration->getExampleChannelsFilePath(), + 'chat' => $this->pathConfiguration->getChatChannelsFilePath() + ), + 'pathConfiguration' => array( + 'example' => $this->pathConfiguration->getExampleConfigurationFilePath(), + 'chat' => $this->pathConfiguration->getChatConfigurationFilePath() + ), + 'users' => array( + 'example' => $this->pathConfiguration->getExampleUsersFilePath(), + 'chat' => $this->pathConfiguration->getChatUsersFilePath() + ), + 'version' => array( + 'example' => $this->pathConfiguration->getExampleVersionFilePath(), + 'chat' => $this->pathConfiguration->getChatVersionFilePath() + ), + ); + + foreach ($identifierToPaths as $identifier => $paths) { + if (!$this->filesystem->isFile($paths['chat'])) { + $directoryPath = dirname($paths['chat']); + + if (!$this->filesystem->isDirectory($directoryPath)) { + $this->output->addLine('no directory available at "' . $directoryPath . '", will create one ...'); + $this->filesystem->createDirectory($directoryPath); + } + $this->output->addLine('no ' . $identifier . ' file available, will create one ...'); + $this->filesystem->copy( + $paths['example'], + $paths['chat'] + ); + } else { + $this->output->addLine($identifier . ' file available, nothing to do ...'); + } + } + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + } +}
\ No newline at end of file diff --git a/source/Command/Restore.php b/source/Command/Restore.php new file mode 100644 index 0000000..60c78a7 --- /dev/null +++ b/source/Command/Restore.php @@ -0,0 +1,152 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-17 + */ + +/** + * Class Command_Restore + */ +class Command_Restore extends Command_AbstractCommand +{ + /** + * @var string + */ + private $command; + + /** + * @var array + */ + private $commands = array( + 'all', + 'channels', + 'application', + 'users', + 'version' + ); + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @var Configuration_Path + */ + private $pathConfiguration; + + /** + * @param Configuration_Path $configuration + */ + public function setPathConfiguration(Configuration_Path $configuration) + { + $this->pathConfiguration = $configuration; + } + + /** + * @param Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @throws Exception + */ + public function execute() + { + $identifiers = array(); + + switch ($this->command) { + case 'all': + $identifiers = array( + 'channels', + 'pathConfiguration', + 'users', + 'version' + ); + break; + case 'channels': + $identifiers[] = 'channels'; + break; + case 'pathConfiguration': + $identifiers[] = 'pathConfiguration'; + break; + case 'users': + $identifiers[] = 'users'; + break; + case 'version': + $identifiers[] = 'version'; + break; + } + + $identifierToPaths = array( + 'channels' => array( + 'backup' => $this->pathConfiguration->getBackupChannelsFilePath(), + 'chat' => $this->pathConfiguration->getChatChannelsFilePath() + ), + 'pathConfiguration' => array( + 'backup' => $this->pathConfiguration->getBackupConfigurationFilePath(), + 'chat' => $this->pathConfiguration->getChatConfigurationFilePath() + ), + 'users' => array( + 'backup' => $this->pathConfiguration->getBackupUsersFilePath(), + 'chat' => $this->pathConfiguration->getChatUsersFilePath() + ), + 'version' => array( + 'backup' => $this->pathConfiguration->getBackupVersionFilePath(), + 'chat' => $this->pathConfiguration->getChatVersionFilePath() + ), + ); + + foreach ($identifiers as $identifier) { + if ($this->filesystem->isFile($identifierToPaths[$identifier]['backup'])) { + $this->output->addLine($identifier . ' backup file available, will restore it ...'); + $this->filesystem->copy( + $identifierToPaths[$identifier]['backup'], + $identifierToPaths[$identifier]['chat'] + ); + } else { + $this->output->addLine('no ' . $identifier .' backup file available ...'); + } + } + } + + /** + * @return array + */ + public function getUsage() + { + return array( + '[--' . implode('|--', $this->commands) . ']' + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + foreach ($this->commands as $command) { + if ($this->input->hasLongOption($command)) { + $this->command = $command; + break; + } + } + + if (is_null($this->command)) { + throw new Exception( + 'invalid command provided' + ); + } + + $this->command = $command; + } +}
\ No newline at end of file diff --git a/source/Command/Upgrade.php b/source/Command/Upgrade.php new file mode 100644 index 0000000..16ac2e6 --- /dev/null +++ b/source/Command/Upgrade.php @@ -0,0 +1,227 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-17 + */ + +/** + * Class Command_Upgrade + */ +class Command_Upgrade extends Command_AbstractCommand +{ + /** + * @var AbstractApplication + */ + private $application; + + /** + * @var File + */ + private $changeLog; + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @var Configuration_Path + */ + private $pathConfiguration; + + /** + * @param AbstractApplication $application + */ + public function setApplication(AbstractApplication $application) + { + $this->application = $application; + } + + /** + * @param File $file + */ + public function setChangeLog(File $file) + { + $this->changeLog = $file; + } + + /** + * @param Configuration_Path $configuration + */ + public function setPathConfiguration(Configuration_Path $configuration) + { + $this->pathConfiguration = $configuration; + } + + /** + * @param Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @throws Exception + */ + public function execute() + { + //__process + $currentVersion = $this->application->getCurrentVersion(); + $finalVersion = $this->application->getExampleVersion(); + $releases = $this->fetchReleasesToProcess($currentVersion, $finalVersion); + + if (empty($releases)) { + $this->output->addLine('nothing to do ...'); + } else { + $pathToIndexPhp = $this->pathConfiguration->getChatPath() . DIRECTORY_SEPARATOR . 'index.php'; + $pathToBackupOfIndexPhp = $pathToIndexPhp . '.backup'; + + $this->backup(); + $this->executePreUpdateTasks($pathToIndexPhp, $pathToBackupOfIndexPhp); + $this->upgrade($releases); + $this->executePostUpdateTasks($pathToIndexPhp, $pathToBackupOfIndexPhp); + + $this->output->addLine('upgraded from "' . $currentVersion . '" to "' . $finalVersion . '"'); + } + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + } + + /** + * @throws Exception + */ + private function backup() + { + $command = $this->application->getBackupCommand(); + + $this->input->setArguments(array('--all')); + + $command->setInput($this->input); + $command->setOutput($this->output); + $command->verify(); + $command->execute(); + } + + /** + * @param string $pathToIndexPhp + * @param string $pathToBackupOfIndexPhp + * @throws Exception + */ + private function executePreUpdateTasks($pathToIndexPhp, $pathToBackupOfIndexPhp) + { + $this->filesystem->move($pathToIndexPhp, $pathToBackupOfIndexPhp); + $file = $this->filesystem->createFile($pathToIndexPhp); + $file->write(array( + '<?php', + 'echo \'updating system, back soon\';' + )); + } + + /** + * @param string $pathToIndexPhp + * @param string $pathToBackupOfIndexPhp + * @throws Exception + */ + private function executePostUpdateTasks($pathToIndexPhp, $pathToBackupOfIndexPhp) + { + $this->filesystem->move($pathToBackupOfIndexPhp, $pathToIndexPhp); + $this->filesystem->copy( + $this->pathConfiguration->getExampleVersionFilePath(), + $this->pathConfiguration->getChatVersionFilePath() + ); + } + + private function upgrade(array $releases) + { + foreach ($releases as $release) { + $path = $this->pathConfiguration->getReleasePath() . DIRECTORY_SEPARATOR . $release; + $files = $this->fetchUpdatesFilesFromRelease($path); + foreach ($files as $file) { + $this->executeUpdateFile($file); + $this->changeLog->append( + '[' . date('Y-m-d H:i:s', time()) . '] ' . + $file + ); + } + } + } + + /** + * @param string $currentVersion + * @param string $finalVersion + * @return array + * @throws Exception + */ + private function fetchReleasesToProcess($currentVersion, $finalVersion) + { + if (!is_dir($this->pathConfiguration->getReleasePath())) { + throw new Exception( + 'no directory found:' . PHP_EOL . + $this->pathConfiguration->getReleasePath() + ); + } + + $releases = $this->filesystem->getDirectories( + $this->pathConfiguration->getReleasePath(), + array('upcoming'), + false + ); + $releasesToProcess = array(); + foreach ($releases as $release) { + $isNewer = version_compare($release, $currentVersion, '>'); + if ($isNewer) { + $releasesToProcess[] = $release; + } + } + natsort($releasesToProcess); + + return $releasesToProcess; + } + + /** + * @param string $path + * @return array + * @throws Exception + */ + private function fetchUpdatesFilesFromRelease($path) + { + if (!is_dir($path)) { + throw new Exception( + 'provided release path is not a directory: ' . PHP_EOL . $path + ); + } + + $files = $this->filesystem->getFiles($path); + + return $files; + } + + /** + * @param string $file + * @throws Exception + */ + private function executeUpdateFile($file) + { + if (!is_file($file)) { + throw new Exception( + 'provided path "' . $file . '" is not a file' + ); + } + + require $file; + } +}
\ No newline at end of file diff --git a/source/Command/User.php b/source/Command/User.php new file mode 100644 index 0000000..616eb85 --- /dev/null +++ b/source/Command/User.php @@ -0,0 +1,144 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-17 + */ + +/** + * Class Command_User + */ +class Command_User extends Command_AbstractCommand +{ + /** + * @var AbstractApplication + */ + private $application; + + /** + * @var string + */ + private $command; + + /** + * @var array + */ + private $commands = array( + 'add', + 'edit', + 'delete', + 'list' + ); + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @var Configuration_Path + */ + private $pathConfiguration; + + /** + * @param AbstractApplication $application + */ + public function setApplication(AbstractApplication $application) + { + $this->application = $application; + } + + /** + * @param Configuration_Path $configuration + */ + public function setPathConfiguration(Configuration_Path $configuration) + { + $this->pathConfiguration = $configuration; + } + + /** + * @param \Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @throws Exception + */ + public function execute() + { + switch ($this->command) { + case 'add': + $command = $this->application->getUserAddCommand(); + break; + case 'edit': + $command = $this->application->getUserEditCommand(); + break; + case 'delete': + $command = $this->application->getUserDeleteCommand(); + break; + case 'list': + $command = $this->application->getUserListCommand(); + break; + default: + throw new Exception( + 'unsupported command "' . $this->command . '"' + ); + } + + $command->setInput($this->input); + $command->setOutput($this->output); + + try { + $command->verify(); + } catch (Exception $exception) { + throw new Exception( + '--' . $this->command . ' ' . implode("\n", $command->getUsage()) . PHP_EOL . + PHP_EOL . + $exception->getMessage() + ); + } + $command->execute(); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + '[--' . implode('|--', $this->commands) . ']' + ); + } + + /** + * @throws Exception + */ + public function verify() + { + $this->command = null; + + if ($this->input->getNumberOfArguments() < 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + + foreach ($this->commands as $command) { + if ($this->input->hasLongOption($command)) { + $this->command = $command; + break; + } + } + + if (is_null($this->command)) { + throw new Exception( + 'invalid command provided' + ); + } + + $this->input->removeLongOption($this->command); + } +}
\ No newline at end of file diff --git a/source/Command/User/AbstractCommand.php b/source/Command/User/AbstractCommand.php new file mode 100644 index 0000000..00512c2 --- /dev/null +++ b/source/Command/User/AbstractCommand.php @@ -0,0 +1,63 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Class Command_User_AbstractCommand + */ +abstract class Command_User_AbstractCommand extends Command_AbstractCommand implements Command_User_CommandInterface +{ + /** + * @var array + */ + protected $channels; + + /** + * @var array + */ + protected $roles; + + /** + * @var File + */ + protected $file; + + /** + * @var array + */ + protected $users; + + /** + * @param array $channels + */ + public function setChannels(array $channels) + { + $this->channels = $channels; + } + + /** + * @param array $roles + */ + public function setRoles(array $roles) + { + $this->roles = $roles; + } + + /** + * @param \File $userFile + */ + public function setUserFile(File $userFile) + { + $this->file = $userFile; + } + + /** + * @param array $users + */ + public function setUsers(array $users) + { + $this->users = $users; + } +}
\ No newline at end of file diff --git a/source/Command/User/Add.php b/source/Command/User/Add.php new file mode 100644 index 0000000..088feef --- /dev/null +++ b/source/Command/User/Add.php @@ -0,0 +1,124 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-12 + */ + +/** + * Class Command_User_Add + */ +class Command_User_Add extends Command_User_AbstractCommand +{ + /** + * @var array + */ + private $inputChannels; + + /** + * @var string + */ + private $inputName; + + /** + * @var string + */ + private $inputPassword; + + /** + * @var string + */ + private $inputRole; + + /** + * @throws Exception + */ + public function execute() + { + end($this->users); + $nextKey = (key($this->users) + 1); + reset($this->users); + + $content = $this->file->read(); + + $content[] = '// added - ' . date('Y-m-d H:i:s'); + $content[] = '$users[' . $nextKey . '] = array();'; + $content[] = '$users[' . $nextKey . '][\'userRole\'] = ' . $this->roles[$this->inputRole] . ';'; + $content[] = '$users[' . $nextKey . '][\'userName\'] = \'' . $this->inputName . '\';'; + $content[] = '$users[' . $nextKey . '][\'password\'] = \'' . $this->inputPassword . '\';'; + $content[] = '$users[' . $nextKey . '][\'channels\'] = array(' . implode(',', $this->inputChannels) . ');'; + + $this->file->write($content); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'name="<name>" password="<password>" role=<id> channels="<id>[,<id>[...]]"', + ' available channels: ' . implode(',', array_keys($this->channels)), + ' available roles: ' . implode(',', array_keys($this->roles)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 4) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $channels = explode(',', $this->input->getParameterValue('channels', '')); + $name = $this->input->getParameterValue('name'); + $password = $this->input->getParameterValue('password'); + $role = $this->input->getParameterValue('role'); + + if (is_null($name)) { + throw new Exception( + 'no name "' . $name . '" provided' + ); + } + + if (is_null($role)) { + throw new Exception( + 'no role "' . $role . '" provided' + ); + } else { + if (!isset($this->roles[$role])) { + throw new Exception( + 'invalid role "' . $role . '" provided' + ); + } + } + + if (is_null($password)) { + throw new Exception( + 'no password "' . $password . '" provided' + ); + } + + if (empty($channels)) { + throw new Exception( + 'no channels provided' + ); + } + + foreach ($channels as $channel) { + if (!isset($this->channels[$channel])) { + throw new Exception( + 'invalid channel "' . $channel . '" provided' + ); + } + } + + $this->inputChannels = $channels; + $this->inputName = $name; + $this->inputPassword = $password; + $this->inputRole = $role; + } +} diff --git a/source/Command/User/CommandInterface.php b/source/Command/User/CommandInterface.php new file mode 100644 index 0000000..b8aa332 --- /dev/null +++ b/source/Command/User/CommandInterface.php @@ -0,0 +1,31 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-14 + */ + +/** + * Interface Command_User_CommandInterface + */ +interface Command_User_CommandInterface extends Command_CommandInterface +{ + /** + * @param array $channels + */ + public function setChannels(array $channels); + + /** + * @param array $roles + */ + public function setRoles(array $roles); + + /** + * @param \File $userFile + */ + public function setUserFile(File $userFile); + + /** + * @param array $users + */ + public function setUsers(array $users); +}
\ No newline at end of file diff --git a/source/Command/User/Delete.php b/source/Command/User/Delete.php new file mode 100644 index 0000000..aeed24a --- /dev/null +++ b/source/Command/User/Delete.php @@ -0,0 +1,105 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-12 + */ + +/** + * Class Command_User_Delete + */ +class Command_User_Delete extends Command_User_AbstractCommand +{ + /** + * @var int + */ + private $inputId; + + /** + * @throws Exception + */ + public function execute() + { + reset($this->users); + + $lines = $this->file->read(); + $content = array(); + + foreach ($lines as $line) { + if ($line == '$users[0][\'channels\'] = array(0);') { + $content[] = $line; + $content[] = ''; + break; + } else { + $content[] = $line; + } + } + + unset($this->users[0]); + + if (empty($this->users)) { + throw new Exception( + 'nothing to delete' + ); + } else { + unset($this->users[$this->inputId]); + $idToUser = array_values($this->users); + + if (!empty($idToUser)) { + foreach ($idToUser as $id => $user) { + ++$id; //we have to increase by one since we have to prevent overwriting the "0" user + $content[] = '// updated - ' . date('Y-m-d H:i:s'); + $content[] = '$users[' . $id . '] = array();'; + $content[] = '$users[' . $id . '][\'userRole\'] = ' . $this->roles[$user['userRole']] . ';'; + $content[] = '$users[' . $id . '][\'userName\'] = \'' . $user['userName'] . '\';'; + $content[] = '$users[' . $id . '][\'password\'] = \'' . $user['password'] . '\';'; + $content[] = '$users[' . $id . '][\'channels\'] = array(' . implode(',', $user['channels']) . ');'; + } + } + + $this->file->write($content); + } + } + + /** + * @return array + */ + public function getUsage() + { + $users = $this->users; + unset($users[0]); + + return array( + 'userid=<id>', + ' available users: ' . implode(',', array_keys($users)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 1) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $validIds = array_keys($this->users); + $userId = $this->input->getParameterValue('user_id'); + + if (!isset($validIds[$userId])) { + throw new Exception( + 'invalid id "' . $userId . '" provided' + ); + } + + if ($userId === 0) { + throw new Exception( + 'you are not allowed to delete id "' . $userId . '"' + ); + } + + $this->inputId = $userId; + } +} diff --git a/source/Command/User/Edit.php b/source/Command/User/Edit.php new file mode 100644 index 0000000..72467ee --- /dev/null +++ b/source/Command/User/Edit.php @@ -0,0 +1,161 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-13 + */ + +/** + * Class Command_User_Edit + */ +class Command_User_Edit extends Command_User_AbstractCommand +{ + /** + * @var array + */ + private $inputChannels; + + /** + * @var string + */ + private $inputName; + + /** + * @var string + */ + private $inputPassword; + + /** + * @var string + */ + private $inputRole; + + /** + * @var int + */ + private $inputId; + + /** + * @throws Exception + */ + public function execute() + { + reset($this->users); + + $lines = $this->file->read(); + $content = array(); + $contentAfterCurrentUser = array(); + $string = new String(); + + $foundCurrentUserContent = false; + $linePrefixToSearchFor = '$users[' . $this->inputId . ']['; + + foreach ($lines as $line) { + if ($string->startsWith($line, $linePrefixToSearchFor)) { + $foundCurrentUserContent = true; + } else { + if ($foundCurrentUserContent) { + $contentAfterCurrentUser[] = $line; + } else { + $content[] = $line; + } + } + } + + $content[] = '$users[' . $this->inputId . '][\'userRole\'] = ' . $this->roles[$this->inputRole] . ';'; + $content[] = '$users[' . $this->inputId . '][\'userName\'] = \'' . $this->inputName . '\';'; + $content[] = '$users[' . $this->inputId . '][\'password\'] = \'' . $this->inputPassword . '\';'; + $content[] = '$users[' . $this->inputId . '][\'channels\'] = array(' . implode(',', $this->inputChannels) . ');'; + + foreach ($contentAfterCurrentUser as $line) { + $content[] = $line; + } + + $this->file->write($content); + } + + /** + * @return array + */ + public function getUsage() + { + return array( + 'user_id=<id> name="<name>" password="<password>" role=<id> channels="<id>[,<id>[...]]"', + ' available channels: ' . implode(',', array_keys($this->channels)), + ' available roles: ' . implode(',', array_keys($this->roles)), + ' available users: ' . implode(',', array_keys($this->users)) + ); + } + + /** + * @throws Exception + */ + public function verify() + { + if ($this->input->getNumberOfArguments() !== 5) { + throw new Exception( + 'invalid number of arguments provided' + ); + } + + $channels = explode(',', $this->input->getParameterValue('channels', '')); + $name = $this->input->getParameterValue('name'); + $password = $this->input->getParameterValue('password'); + $role = $this->input->getParameterValue('role'); + $userId = $this->input->getParameterValue('user_id'); + $validIds = array_keys($this->users); + + if (is_null($name)) { + throw new Exception( + 'no name provided' + ); + } + + if (is_null($role)) { + throw new Exception( + 'no role provided' + ); + } else { + if (!isset($this->roles[$role])) { + throw new Exception( + 'invalid role "' . $role . '" provided' + ); + } + } + + if (is_null($password)) { + throw new Exception( + 'no password provided' + ); + } + + if (empty($channels)) { + throw new Exception( + 'no channels provided' + ); + } + + foreach ($channels as $channel) { + if (!isset($this->channels[$channel])) { + throw new Exception( + 'invalid channel "' . $channel . '" provided' + ); + } + } + + if (($userId === 0)) { + throw new Exception( + 'no id provided' + ); + } else if (!isset($validIds[$userId])) { + throw new Exception( + 'invalid id provided' + ); + } + + $this->inputChannels = $channels; + $this->inputName = $name; + $this->inputPassword = $password; + $this->inputRole = $role; + $this->inputId = $userId; + } +}
\ No newline at end of file diff --git a/source/Command/User/List.php b/source/Command/User/List.php new file mode 100644 index 0000000..130e842 --- /dev/null +++ b/source/Command/User/List.php @@ -0,0 +1,58 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-12 + */ + +/** + * Class Command_User_List + */ +class Command_User_List extends Command_User_AbstractCommand +{ + /** + * @throws Exception + */ + public function execute() + { + unset($this->users[0]); + //remove initial user since we are not allowed to remove this user + $numberOfUsers = count($this->users); + + $this->output->addLine('number of users: ' . $numberOfUsers); + + if ($numberOfUsers > 0) { + $this->output->addLine(); + $this->output->addLine('id | name | role | channels'); + $this->output->addLine('----------------'); + + foreach ($this->users as $id => $user) { + $this->output->addLine( + implode( + ' | ', + array( + $id, + $user['userName'], + $user['userRole'], + implode(',', $user['channels']) + ) + ) + ); + } + } + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + } +} diff --git a/source/Command/VerifyInstallation.php b/source/Command/VerifyInstallation.php new file mode 100644 index 0000000..0abd881 --- /dev/null +++ b/source/Command/VerifyInstallation.php @@ -0,0 +1,69 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-17 + */ + +/** + * Class Command_VerifyInstallation + */ +class Command_VerifyInstallation extends Command_AbstractCommand +{ + /** + * @var AbstractApplication + */ + protected $application; + + /** + * @param AbstractApplication $application + */ + public function setApplication(AbstractApplication $application) + { + $this->application = $application; + } + + /** + * @throws Exception + */ + public function execute() + { + $this->verifyLocalFiles($this->application, $this->input, $this->output); + $this->verifyVersion($this->application, $this->input, $this->output); + //@todo fetch current version from url + } + + /** + * @param AbstractApplication $application + */ + private function verifyLocalFiles(AbstractApplication $application) + { + $command = $application->getVerifyInstallationLocalFilesCommand(); + $command->verify(); + $command->execute(); + } + + /** + * @param AbstractApplication $application + */ + private function verifyVersion(AbstractApplication $application) + { + $command = $application->getVerifyInstallationVersionCommand(); + $command->verify(); + $command->execute(); + } + + /** + * @return array + */ + public function getUsage() + { + return array(); + } + + /** + * @throws Exception + */ + public function verify() + { + } +}
\ No newline at end of file diff --git a/source/Command/VerifyInstallation/AbstractCommand.php b/source/Command/VerifyInstallation/AbstractCommand.php new file mode 100644 index 0000000..d64a680 --- /dev/null +++ b/source/Command/VerifyInstallation/AbstractCommand.php @@ -0,0 +1,50 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-21 + */ + +/** + * Class Command_VerifyInstallation_AbstractCommand + */ +abstract class Command_VerifyInstallation_AbstractCommand extends Command_AbstractCommand +{ + /** + * @var AbstractApplication + */ + protected $application; + + /** + * @var Filesystem + */ + protected $filesystem; + + /** + * @var Configuration_Path + */ + protected $pathConfiguration; + + /** + * @param AbstractApplication $application + */ + public function setApplication(AbstractApplication $application) + { + $this->application = $application; + } + + /** + * @param Filesystem $filesystem + */ + public function setFilesystem(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @param Configuration_Path $configuration + */ + public function setPathConfiguration(Configuration_Path $configuration) + { + $this->pathConfiguration = $configuration; + } +}
\ No newline at end of file diff --git a/source/Command/VerifyInstallation/LocalFiles.php b/source/Command/VerifyInstallation/LocalFiles.php new file mode 100644 index 0000000..dafd44a --- /dev/null +++ b/source/Command/VerifyInstallation/LocalFiles.php @@ -0,0 +1,70 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-21 + */ + +/** + * Class Command_VerifyInstallation_LocalFiles + */ +class Command_VerifyInstallation_LocalFiles extends Command_VerifyInstallation_AbstractCommand +{ + /** + * @throws Exception + */ + public function execute() + { + $pathToLibDirectory = $this->pathConfiguration->getChatPath() . DIRECTORY_SEPARATOR . 'lib'; + + if (!$this->filesystem->isDirectory($pathToLibDirectory)) { + throw new Exception( + 'directory "lib" is missing, installation needed' + ); + } + + $pathToDataDirectory = $pathToLibDirectory . DIRECTORY_SEPARATOR . 'data'; + + if (!$this->filesystem->isDirectory($pathToDataDirectory)) { + throw new Exception( + 'directory "data" is missing, installation needed' + ); + } + + $identifierToPublicPath = array( + 'channels' => $this->pathConfiguration->getChatChannelsFilePath(), + 'pathConfiguration' => $this->pathConfiguration->getChatConfigurationFilePath(), + 'users' => $this->pathConfiguration->getChatUsersFilePath(), + 'version' => $this->pathConfiguration->getChatVersionFilePath() + ); + + foreach ($identifierToPublicPath as $identifier => $path) { + if (!$this->filesystem->isFile($path)) { + throw new Exception( + 'file "' . $identifier . '" is missing, installation needed' + ); + } + } + + $pathToInstallPhp = $this->pathConfiguration->getChatPath() . DIRECTORY_SEPARATOR . 'install.php'; + + if ($this->filesystem->isFile($pathToInstallPhp)) { + throw new Exception( + 'file "' . $pathToInstallPhp . '" still available' + ); + } + } + + /** + * @return array + */ + public function getUsage() + { + } + + /** + * @throws Exception + */ + public function verify() + { + } +}
\ No newline at end of file diff --git a/source/Command/VerifyInstallation/Version.php b/source/Command/VerifyInstallation/Version.php new file mode 100644 index 0000000..a2f592b --- /dev/null +++ b/source/Command/VerifyInstallation/Version.php @@ -0,0 +1,40 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-21 + */ + +/** + * Class Command_VerifyInstallation_Version + */ +class Command_VerifyInstallation_Version extends Command_VerifyInstallation_AbstractCommand +{ + /** + * @throws Exception + */ + public function execute() + { + $exampleVersion = $this->application->getExampleVersion(); + $currentVersion = $this->application->getCurrentVersion(); + + if ($exampleVersion !== $currentVersion) { + throw new Exception( + 'current version "' . $currentVersion . '" and code version "' . $exampleVersion . '" differs, update needed' + ); + } + } + + /** + * @return array + */ + public function getUsage() + { + } + + /** + * @throws Exception + */ + public function verify() + { + } +}
\ No newline at end of file |