Replace Core\Cli by Symfony\Console

This commit is contained in:
Frédéric Guillot
2014-11-29 12:28:35 -05:00
parent d987916128
commit e8fa25f9ca
7 changed files with 224 additions and 146 deletions

53
app/Console/Base.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
namespace Console;
use Core\Tool;
use Pimple\Container;
use Symfony\Component\Console\Command\Command;
/**
* Base command class
*
* @package console
* @author Frederic Guillot
*
* @property \Model\Notification $notification
* @property \Model\Task $task
* @property \Model\TaskExport $taskExport
* @property \Model\TaskFinder $taskFinder
*/
abstract class Base extends Command
{
/**
* Container instance
*
* @access protected
* @var \Pimple\Container
*/
protected $container;
/**
* Constructor
*
* @access public
* @param \Pimple\Container $container
*/
public function __construct(Container $container)
{
parent::__construct();
$this->container = $container;
}
/**
* Load automatically models
*
* @access public
* @param string $name Model name
* @return mixed
*/
public function __get($name)
{
return Tool::loadModel($this->container, $name);
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Console;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class OverdueNotification extends Base
{
protected function configure()
{
$this
->setName('notification:overdue-tasks')
->setDescription('Send notifications for overdue tasks')
->addOption('show', null, InputOption::VALUE_NONE, 'Show sent overdue tasks');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$projects = array();
$tasks = $this->taskFinder->getOverdueTasks();
// Group tasks by project
foreach ($tasks as $task) {
$projects[$task['project_id']][] = $task;
}
// Send notifications for each project
foreach ($projects as $project_id => $project_tasks) {
$users = $this->notification->getUsersList($project_id);
$this->notification->sendEmails(
'task_due',
$users,
array('tasks' => $project_tasks, 'project' => $project_tasks[0]['project_name'])
);
}
if ($input->getOption('show')) {
$this->showTable($output, $tasks);
}
}
public function showTable(OutputInterface $output, array $tasks)
{
$rows = array();
foreach ($tasks as $task) {
$rows[] = array(
$task['id'],
$task['title'],
date('Y-m-d', $task['date_due']),
$task['project_id'],
$task['project_name'],
$task['assignee_name'] ?: $task['assignee_username'],
);
}
$table = new Table($output);
$table
->setHeaders(array('Id', 'Title', 'Due date', 'Project Id', 'Project name', 'Assignee'))
->setRows($rows)
->render();
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Console;
use Core\Tool;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TaskExport extends Base
{
protected function configure()
{
$this
->setName('export:tasks')
->setDescription('Tasks export (CSV)')
->addArgument('project_id', InputArgument::REQUIRED, 'Project id')
->addArgument('start_date', InputArgument::REQUIRED, 'Start date (YYYY-MM-DD)')
->addArgument('end_date', InputArgument::REQUIRED, 'End date (YYYY-MM-DD)');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->taskExport->export(
$input->getArgument('project_id'),
$input->getArgument('start_date'),
$input->getArgument('end_date')
);
if (is_array($data)) {
Tool::csv($data);
}
}
}

View File

@@ -1,75 +0,0 @@
<?php
namespace Core;
use Closure;
/**
* CLI class
*
* @package core
* @author Frederic Guillot
*/
class Cli
{
/**
* Default command name
*
* @access public
* @var string
*/
public $default_command = 'help';
/**
* List of registered commands
*
* @access private
* @var array
*/
private $commands = array();
/**
*
*
* @access public
* @param string $command Command name
* @param Closure $callback Command callback
*/
public function register($command, Closure $callback)
{
$this->commands[$command] = $callback;
}
/**
* Execute a command
*
* @access public
* @param string $command Command name
*/
public function call($command)
{
if (isset($this->commands[$command])) {
$this->commands[$command]();
exit;
}
}
/**
* Determine which command to execute
*
* @access public
*/
public function execute()
{
if (php_sapi_name() !== 'cli') {
die('This script work only from the command line.');
}
if ($GLOBALS['argc'] === 1) {
$this->call($this->default_command);
}
$this->call($GLOBALS['argv'][1]);
$this->call($this->default_command);
}
}