Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony/Console #4

Merged
merged 3 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ logs/
composer.lock
build/
composer
composer.phar
15 changes: 15 additions & 0 deletions Tests/spec/Console/ClearCommandSpec.php
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');
}
}
15 changes: 15 additions & 0 deletions Tests/spec/Console/EchoCommandSpec.php
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');
}
}
16 changes: 16 additions & 0 deletions Tests/spec/Console/SynchronizeCommandSpec.php
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');
}

}
136 changes: 8 additions & 128 deletions bin/crontab
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();
8 changes: 2 additions & 6 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</target>

<target name="phpmd-local">
<exec dir="${project.basedir}" executable="${bin}/phpmd" output="${build}/phpmd/report.html" checkreturn="true">
<exec dir="${project.basedir}" executable="${bin}/phpmd" output="${build}/phpmd/report.html" checkreturn="true" passthru="true">
<arg line="${src}"/>
<arg line="html"/>
<arg line="${project.basedir}/phpmd.xml"/>
Expand Down Expand Up @@ -140,13 +140,9 @@
</exec>
</target>

<target name="box">
<exec/>
</target>

<target name="tests" depends="phpmd, phpcs, phploc, phpcpd, phpspec"/>
<target name="tests-local" depends="phpmd-local, phpspec-local, phpcs, phploc-local, phpcpd"/>

<target name="build" depends="composer,prepare,tests-local,box"/>
<target name="build" depends="composer,prepare,tests-local"/>
<target name="continous" depends="prepare,tests"/>
</project>
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
},
"require": {
"php": ">=5.3.3",
"sebastian/phpcpd": "^2.0",
"sebastian/phpdcd": "^1.0"
"symfony/console": ">=2.3"
},
"require-dev": {
"phpunit/phpunit" : ">=4.0",
"sebastian/phpcpd": "^2.0",
"sebastian/phpdcd": "^1.0",
"phpspec/phpspec": "~2.4",
"henrikbjorn/phpspec-code-coverage": "*",
"symfony/yaml": ">=2.3",
Expand Down
98 changes: 98 additions & 0 deletions src/Console/AbstractCommand.php
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;
}
}
65 changes: 65 additions & 0 deletions src/Console/ClearCommand.php
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");
}
}
Loading