Exemplo n.º 1
0
Arquivo: Model.php Projeto: rj28/test
 public function execute(Console $console)
 {
     $this->getWriteConnection()->execute("\n\t\t\tupdate {$this->getSource()} set executed_at = ? where cron_job_id = ?\n\t\t", [$this->executed_at = date("Y-m-d H:i:s"), $this->cron_job_id]);
     try {
         $console->handle(array('task' => $this->task_name, 'action' => $this->action_name));
         $this->save(['last_message' => '', 'completed_at' => date('Y-m-d H:i:s'), 'locked_till' => time() + $this->delay]);
         Assert::saved($this);
         return static::RESULT_OK;
     } catch (Exception $e) {
         $this->getLogger()->exception($e);
         $this->save(['last_message' => get_class($e) . ': ' . $e->getMessage(), 'completed_at' => null, 'locked_till' => time() + $this->delay]);
         return static::RESULT_ERR;
     }
 }
Exemplo n.º 2
0
 /**
  * Handle the whole command-line tasks
  *
  * @param array $arguments Cli arguments
  *
  * @return mixed
  * @throws \Phalcon\Cli\Console\Exception
  */
 public function handle(array $arguments = null)
 {
     if (isset($arguments['task']) && in_array($arguments['task'], array('-h', '--help', 'help'))) {
         $this->setTasksDir();
         $this->createHelp();
         $this->showHelp();
         return;
     } elseif (isset($arguments['action']) && in_array($arguments['action'], array('-h', '--help', 'help'))) {
         $this->setTasksDir();
         $this->createHelp();
         $this->showTaskHelp($arguments['task']);
         return;
     }
     parent::handle($arguments);
 }
Exemplo n.º 3
0
 /**
  * handles the Console application and starts the Phalcon Module
  *
  * @param array  $arguments
  */
 public function handle($arguments = null)
 {
     $config = $this->getDI()->getConfig();
     $this->progPath = array_shift($arguments['params']);
     $this->cmd = array_shift($arguments['params']);
     if (is_null($this->cmd)) {
         if (!isset($arguments['defaultCmd'])) {
             throw new \Exception('The Console was not given a command', 1);
         }
         $this->cmd = $arguments['defaultCmd'];
     }
     if (in_array($this->cmd, ['help', '--help', '-h'])) {
         $this->printCmdList();
         exit(0);
     }
     if (strpos($this->cmd, '.') !== false || strpos($this->cmd, '/') !== false) {
         throw new \Exception('Invalid command name', 1);
     }
     // All environments
     $cmdArr = (require "{$config->path->modulesDir}/{$arguments['module']}/cmd.php");
     if (DEV_ENV === ENV) {
         $devCmdArr = (require "{$config->dev->path->devDir}/cmd_overrides.php");
         $cmdArr = array_replace($cmdArr, $devCmdArr);
     }
     if (!array_key_exists($this->cmd, $cmdArr)) {
         throw new \Exception('The command description does not exist', 1);
     }
     $taskParts = explode('::', $cmdArr[$this->cmd]);
     $task = $taskParts[0];
     $action = isset($taskParts[1]) ? $taskParts[1] : 'main';
     try {
         parent::handle(['module' => $arguments['module'], 'task' => 'Webird\\Cli\\Tasks\\' . ucfirst($task), 'action' => $action, 'params' => $arguments['params']]);
     } catch (ArgumentValidationException $e) {
         $this->printHelpRecommend($e->getMessage());
         exit(1);
     } catch (PrintHelpException $e) {
         if ($e->getCode() == 1) {
             $this->printHelpRecommend($e->getMessage());
             exit(1);
         } else {
             $this->printHelp($e->getCmdDef(), $e->getSpecs());
             exit(0);
         }
     }
 }
Exemplo n.º 4
0
}
// Create a console application
$console = new ConsoleApp();
$console->setDI($di);
//Setup the database service
$di->set('db', function () use($config) {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname));
});
/**
 * Process the console arguments
 */
$arguments = array();
foreach ($argv as $k => $arg) {
    if ($k == 1) {
        $arguments['task'] = $arg;
    } elseif ($k == 2) {
        $arguments['action'] = $arg;
    } elseif ($k >= 3) {
        $arguments['params'][] = $arg;
    }
}
// Define global constants for the current task and action
define('CURRENT_TASK', isset($argv[1]) ? $argv[1] : null);
define('CURRENT_ACTION', isset($argv[2]) ? $argv[2] : null);
try {
    // Handle incoming arguments
    $console->handle($arguments);
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
    exit(255);
}
Exemplo n.º 5
0
 /**
  * Handle the command-line arguments
  *
  * @package     las
  * @version     1.0
  *
  * @param mixed $arguments
  */
 public function handle($arguments = null)
 {
     $params = array();
     switch (count($arguments)) {
         case 1:
             $task = 'main';
             $action = 'main';
             break;
         case 2:
             $task = $arguments[1];
             $action = 'main';
             break;
         case 3:
             $task = $arguments[1];
             $action = $arguments[2];
             break;
         default:
             $task = $arguments[1];
             $action = $arguments[2];
             $params = array_slice($arguments, 3);
             break;
     }
     $arguments = array_merge(array('module' => 'cli', 'task' => $task, 'action' => $action), $params);
     parent::handle($arguments);
 }
Exemplo n.º 6
0
    $loader->registerDirs(array(APP_PATH . '/tasks', APP_PATH . '/models', APP_PATH . '/library', APP_PATH . '/library/PHPExcel/Classes/'));
    $loader->register();
    // Load the conf
    if (is_readable(APP_PATH . '/config/config.php')) {
        $config = (include APP_PATH . '/config/config.php');
        $di->set('config', $config);
    }
    // Mongo connection
    $di->set('mongo', function () use($config) {
        if (!$config->mongo->user or !$config->mongo->pass) {
            $mongo = new MongoClient('mongodb://' . $config->mongo->host);
        } else {
            $mongo = new MongoClient("mongodb://" . $config->mongo->user . ":" . $config->mongo->pass . "@" . $config->mongo->host, array("db" => $config->mongo->dbname));
        }
        return $mongo->selectDb($config->mongo->dbname);
    }, false);
    // Load collection nanager
    $di->set('collectionManager', function () {
        return new \Phalcon\Mvc\Collection\Manager();
    });
    // Create a console app
    $console = new ConsoleApp();
    $console->setDI($di);
} catch (Exception $e) {
    echo $e->getMessage();
}
try {
    $console->handle();
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
}
Exemplo n.º 7
0
<?php

use Phalcon\Di\FactoryDefault\Cli;
use Phalcon\Cli\Console;
require __DIR__ . '/../vendor/autoload.php';
$di = new Cli();
$di->get('dispatcher')->setNamespaceName('CaioFRAlmeida\\SoccerCompanyEvent\\Task');
$args = [];
if (isset($argv[1])) {
    $args['task'] = $argv[1];
}
$console = new Console($di);
try {
    $console->handle($args);
} catch (\Exception $e) {
    die('erro ao executar task');
}
Exemplo n.º 8
0
 public function testArgumentRouter()
 {
     $this->specify("CLI Console doesn't work with arguments", function () {
         $di = new CliFactoryDefault();
         $di->setShared('router', function () {
             $router = new Router(true);
             return $router;
         });
         $console = new Console();
         $console->setDI($di);
         $dispatcher = $console->getDI()->getShared('dispatcher');
         $console->setArgument(array('php'))->handle();
         expect($dispatcher->getTaskName())->equals('main');
         expect($dispatcher->getActionName())->equals('main');
         expect($dispatcher->getParams())->equals([]);
         expect($dispatcher->getReturnedValue())->equals('mainAction');
         $console->setArgument(array('php', 'echo'))->handle();
         expect($dispatcher->getTaskName())->equals('echo');
         expect($dispatcher->getActionName())->equals('main');
         expect($dispatcher->getParams())->equals([]);
         expect($dispatcher->getReturnedValue())->equals('echoMainAction');
         $console->setArgument(array('php', 'main', 'hello'))->handle();
         expect($dispatcher->getTaskName())->equals('main');
         expect($dispatcher->getActionName())->equals('hello');
         expect($dispatcher->getParams())->equals([]);
         expect($dispatcher->getReturnedValue())->equals('Hello !');
         $console->setArgument(array('php', 'main', 'hello', 'World', '######'))->handle();
         expect($dispatcher->getTaskName())->equals('main');
         expect($dispatcher->getActionName())->equals('hello');
         expect($dispatcher->getParams())->equals(array('World', '######'));
         expect($dispatcher->getReturnedValue())->equals('Hello World######');
     });
     $this->specify("CLI Console doesn't work with arguments (2)", function () {
         $di = new CliFactoryDefault();
         $di->setShared('router', function () {
             $router = new Router(true);
             return $router;
         });
         $console = new Console();
         $console->setDI($di);
         $dispatcher = $console->getDI()->getShared('dispatcher');
         // testing namespace
         $dispatcher->setDefaultNamespace('Dummy\\');
         $console->setArgument(array('php', 'main', 'hello', 'World', '!'));
         $console->handle();
     }, ['throws' => [\Phalcon\Cli\Dispatcher\Exception::class, 'Dummy\\MainTask handler class cannot be loaded']]);
 }
Exemplo n.º 9
0
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);
    $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
    return new $class($dbConfig);
});
// Create a console application
$console = new ConsoleApp();
$console->setDI($di);
/**
 * Process the console arguments
 */
$arguments = array();
foreach ($argv as $k => $arg) {
    if ($k == 1) {
        $arguments['task'] = $arg;
    } elseif ($k == 2) {
        $arguments['action'] = $arg;
    } elseif ($k >= 3) {
        $arguments['params'][] = $arg;
    }
}
// Define global constants for the current task and action
define('CURRENT_TASK', isset($argv[1]) ? $argv[1] : null);
define('CURRENT_ACTION', isset($argv[2]) ? $argv[2] : null);
try {
    // Handle incoming arguments
    $console->handle(array('task' => 'App\\Tasks\\Seed'));
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
    exit(255);
}
            $this->log->info('Каталог бэкапа {folder} отсутствовал, создали его.', ['folder' => $this->folderName]);
        } elseif (is_dir($this->folderName) && is_writable($this->folderName)) {
            $this->log->info('Каталог бэкапа {folder} уже существует и доступен для записи', ['folder' => $this->folderName]);
        } else {
            $this->log->error('Каталог бэкапа {folder} отсутствует или недоступен для создания', ['folder' => $this->folderName]);
        }
    }
}
$di = new CLI();
$di['dispatcher'] = function () {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultTask('Backup');
    $dispatcher->setDefaultAction('backup');
    return $dispatcher;
};
$di['log'] = function () {
    return new Phalcon\Logger\Adapter\Stream('php://stdout');
};
try {
    $console = new Console($di);
    $handleParams = [];
    array_shift($argv);
    foreach ($argv as $param) {
        list($name, $value) = explode('=', $param);
        $handleParams[$name] = $value;
    }
    $console->handle($handleParams);
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
    exit(255);
}
Exemplo n.º 11
0
<?php

use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp;
//Using the CLI factory default services container
$di = new CliDI();
//Create a console application
$console = new ConsoleApp();
$console->setDI($di);
//
$console->handle(array('task' => 'shell_script_name', 'action' => 'echo'));
Exemplo n.º 12
0
<?php

use Phalcon\Exception;
use Phalcon\DI\FactoryDefault\CLI;
use Phalcon\CLI\Console;
date_default_timezone_set('America/New_York');
//load composer dependencies
require __DIR__ . '/../../vendor/autoload.php';
//Using the CLI factory default services container
$di = new CLI();
//load configuration
$config = (include __DIR__ . '/config.php');
//load classes
include __DIR__ . '/loader.php';
//load services class
include __DIR__ . '/services.php';
Services::load($di, $config);
//Create a console application
$console = new Console();
$console->setDI($di);
try {
    $console->handle(array('task' => 'Service', 'action' => 'listen'));
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
    exit(255);
}
Exemplo n.º 13
0
<?php

use Phalcon\CLI\Console as ConsoleApp;
$config = (include "app/config/config.php");
include "app/config/loader.php";
include "app/config/services.php";
$arguments = array();
foreach ($argv as $k => $arg) {
    if ($k == 1) {
        $arguments['task'] = $arg;
    } elseif ($k == 2) {
        $arguments['action'] = $arg;
    } elseif ($k >= 3) {
        $arguments[] = $arg;
    }
}
try {
    $app = new ConsoleApp();
    $app->setDI($di);
    $app->handle($arguments);
} catch (Exception $e) {
    echo $e->getMessage();
}