-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Hexmedia/feature/symfony-command
Symfony/Console
- Loading branch information
Showing
12 changed files
with
295 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ logs/ | |
composer.lock | ||
build/ | ||
composer | ||
composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace spec\Hexmedia\Crontab\Console; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class ClearCommandSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Hexmedia\Crontab\Console\ClearCommand'); | ||
$this->shouldHaveType('Symfony\Component\Console\Command\Command'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace spec\Hexmedia\Crontab\Console; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class EchoCommandSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Hexmedia\Crontab\Console\EchoCommand'); | ||
$this->shouldHaveType('Symfony\Component\Console\Command\Command'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace spec\Hexmedia\Crontab\Console; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class SynchronizeCommandSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Hexmedia\Crontab\Console\SynchronizeCommand'); | ||
$this->shouldHaveType('Symfony\Component\Console\Command\Command'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,13 @@ | ||
#!/usr/bin/php | ||
<?php | ||
|
||
//For easy use. | ||
|
||
use Hexmedia\Crontab\Reader\SystemReader; | ||
use Hexmedia\Crontab\Crontab; | ||
use Hexmedia\Crontab\ReaderFactory; | ||
use Hexmedia\Crontab\Writer\SystemWriter; | ||
|
||
|
||
if (is_file($autoload = getcwd() . '/vendor/autoload.php')) { | ||
require $autoload; | ||
} else if (is_file($autoload = getcwd() . '/../../autoload.php')) { | ||
require $autoload; | ||
} else if (is_file($autoload = __DIR__ . '/../vendor/autoload.php')) { | ||
require($autoload); | ||
} else if (is_file($autoload = __DIR__ . '/../../../autoload.php')) { | ||
require($autoload); | ||
} else { | ||
fwrite(STDERR, | ||
'You must set up the project dependencies, run the following commands:' . PHP_EOL . | ||
'curl -s http://getcomposer.org/installer | php' . PHP_EOL . | ||
'php composer.phar install' . PHP_EOL | ||
); | ||
exit(1); | ||
} | ||
|
||
if ($argc < 2) { | ||
error_log("Too less arguments for crontab."); | ||
} | ||
|
||
$action = null; | ||
$user = null; | ||
$file = null; | ||
$type = 'ini'; | ||
$name = null; | ||
$machine = null; | ||
|
||
foreach ($argv as $i => $arg) { | ||
switch ($arg) { | ||
case "synchronize": | ||
if (null === $action) { | ||
$action = "synchronize"; | ||
} else { | ||
error_log("Cannot call two actions at once"); | ||
} | ||
break; | ||
case "echo": | ||
if (null === $action) { | ||
$action = "echo"; | ||
} else { | ||
error_log("Cannot call two actions at once"); | ||
} | ||
break; | ||
case "--user": | ||
$user = $argv[$i + 1]; | ||
unset($argv[$i + 1]); | ||
break; | ||
case "--file": | ||
$file = $argv[$i + 1]; | ||
unset($argv[$i + 1]); | ||
break; | ||
case "--type": | ||
$type = $argv[$i + 1]; | ||
unset($argv[$i + 1]); | ||
break; | ||
case "--name": | ||
$name = $argv[$i + 1]; | ||
unset($argv[$i + 1]); | ||
break; | ||
case "--machine": | ||
$machine = $argv[$i + 1]; | ||
unset($argv[$i + 1]); | ||
break; | ||
} | ||
} | ||
|
||
if (null === $file) { | ||
error_log("Configuration file not provided."); | ||
exit; | ||
} | ||
|
||
switch ($action) { | ||
case "synchronize": | ||
case "echo": | ||
$conf = array(); | ||
|
||
if (null !== $user) { | ||
$conf['user'] = $user; | ||
} | ||
|
||
$crontab = new Crontab($user, $name); | ||
#!/usr/bin/env php | ||
|
||
if (null !== $machine) { | ||
$conf['machine'] = $machine; | ||
} | ||
|
||
$conf['crontab'] = $crontab; | ||
$conf['type'] = 'unix'; | ||
|
||
$conf['user'] = "./Tests/example_configurations/test.unix"; | ||
|
||
$systemReader = ReaderFactory::create($conf); | ||
|
||
$conf['user'] = $user; | ||
|
||
// $systemReader = new SystemReader($crontab, $conf); | ||
|
||
$conf['file'] = $file; | ||
|
||
$conf['type'] = $type; | ||
|
||
$crontab = $systemReader->read(); | ||
$crontab->clearManagedTasks(); | ||
|
||
$configReader = ReaderFactory::create($conf); | ||
|
||
$crontab = $configReader->read(); | ||
<?php | ||
|
||
$writter = new SystemWriter($conf); | ||
require dirname(__DIR__) . "/vendor/autoload.php"; | ||
|
||
if ($action == "echo") { | ||
echo $writter->toCronFile($crontab); | ||
} else { | ||
$writter->save($crontab); | ||
} | ||
break; | ||
default: | ||
error_log("Unknown action."); | ||
} | ||
$application = new \Symfony\Component\Console\Application(); | ||
|
||
$application->add(new \Hexmedia\Crontab\Console\SynchronizeCommand()); | ||
$application->add(new \Hexmedia\Crontab\Console\EchoCommand()); | ||
$application->add(new \Hexmedia\Crontab\Console\ClearCommand()); | ||
|
||
$application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Hexmedia\Crontab\Console; | ||
|
||
use Hexmedia\Crontab\Crontab; | ||
use Hexmedia\Crontab\Reader\ReaderInterface; | ||
use Hexmedia\Crontab\Reader\SystemReader; | ||
use Hexmedia\Crontab\ReaderFactory; | ||
use Hexmedia\Crontab\Writer\SystemWriter; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
abstract class AbstractCommand extends Command | ||
{ | ||
/** | ||
* | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->addOption("machine", "m", InputOption::VALUE_OPTIONAL, "Machine name to synchronize") | ||
->addOption("user", "u", InputOption::VALUE_OPTIONAL, "Username for synchronization (crontab -u)") | ||
->addOption("type", "t", InputOption::VALUE_REQUIRED, "Type of parsed file, if not given system will guess") | ||
->addOption("dry-run", null, InputOption::VALUE_OPTIONAL, "Do not write crontab file"); | ||
|
||
$this->configureArguments(); | ||
$this->configureName(); | ||
} | ||
|
||
abstract protected function configureName(); | ||
|
||
/** | ||
* | ||
*/ | ||
protected function configureArguments() | ||
{ | ||
$this | ||
->addArgument("configuration-file", InputArgument::REQUIRED, "Configuration file") | ||
->addArgument("name", InputArgument::REQUIRED, "Name of project"); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return null | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$name = $input->getArgument("name"); | ||
$user = $input->getOption("user"); | ||
|
||
$crontab = new Crontab($user, $name); | ||
|
||
$systemReader = new SystemReader($user, $crontab); | ||
|
||
$systemReader->read(); | ||
|
||
$configuration = $this->prepareConfiguration($input); | ||
$configuration['crontab'] = $crontab; | ||
|
||
/** @var ReaderInterface $reader */ | ||
$reader = ReaderFactory::create($configuration); | ||
|
||
$crontab = $reader->read(); | ||
|
||
$this->output($output, $crontab, $user); | ||
} | ||
|
||
/** | ||
* @param OutputInterface $output | ||
* @param Crontab $crontab | ||
* @param string|null $user | ||
* @return mixed | ||
*/ | ||
abstract public function output(OutputInterface $output, Crontab $crontab, $user = null); | ||
|
||
/** | ||
* @param InputInterface $input | ||
* | ||
* @return array | ||
*/ | ||
protected function prepareConfiguration(InputInterface $input) | ||
{ | ||
$configuration = array(); | ||
|
||
$configuration['user'] = $input->getOption('user'); | ||
$configuration['type'] = $input->getOption('type'); | ||
$configuration['file'] = $input->getArgument('configuration-file'); | ||
$configuration['name'] = $input->getArgument('name'); | ||
$configuration['machine'] = $input->getOption('machine'); | ||
|
||
return $configuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Hexmedia\Crontab\Console; | ||
|
||
use Hexmedia\Crontab\Crontab; | ||
use Hexmedia\Crontab\Reader\SystemReader; | ||
use Hexmedia\Crontab\Writer\SystemWriter; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ClearCommand extends AbstractCommand | ||
{ | ||
protected function configureName() | ||
{ | ||
$this | ||
->setName("clear") | ||
->setDescription("Clear this project crontabs from this machine"); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return null | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$name = $input->getArgument("name"); | ||
$user = $input->getOption("user"); | ||
|
||
$crontab = new Crontab($user, $name); | ||
|
||
$systemReader = new SystemReader($user, $crontab); | ||
|
||
$systemReader->read(); | ||
|
||
$crontab->clearManagedTasks(); | ||
|
||
$this->output($output, $crontab, $user); | ||
} | ||
|
||
/** | ||
* @param OutputInterface $output | ||
* @param Crontab $crontab | ||
* @param string|null $user | ||
* @return mixed | ||
*/ | ||
public function output(OutputInterface $output, Crontab $crontab, $user = null) | ||
{ | ||
$crontab->clearManagedTasks(); | ||
|
||
$writer = new SystemWriter(array('user' => $user)); | ||
|
||
$writer->save($crontab); | ||
|
||
$output->writeln("Your crontab was updated!"); | ||
} | ||
|
||
protected function configureArguments() | ||
{ | ||
$this | ||
->addArgument("name", InputArgument::REQUIRED, "Name of project") | ||
->addArgument("configuration-file", InputArgument::OPTIONAL, "Configuration file"); | ||
} | ||
} |
Oops, something went wrong.