<?php $di = new Phalcon\DI\FactoryDefault\CLI(); require_once __DIR__ . '/bootstrap.php'; restore_exception_handler(); restore_error_handler(); //Create a console application $console = new Phalcon\CLI\Console(); $console->setDI($di); if (1 || count($argv) == 3) { $task = (string) $argv[1]; $action = (string) $argv[2]; try { try { $console->handle(array('task' => $task, 'action' => $action, 'id' => (string) @$argv[3], 'file' => (string) @$argv[4])); } catch (Exception $e) { throw $e; } } catch (Exception $e) { echo "Exception " . get_class($e) . " thrown with message: " . $e->getMessage() . "\n"; echo $e->getTraceAsString() . "\n"; exit(1); } } else { echo "Wrong parameter count. Usage: php app/task.php task_name action_name\n"; }
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT); ini_set('display_errors', 1); require dirname(__FILE__) . '/../app/bootstrap.php'; define('VERSION', '1.0.0'); $loader = new \Phalcon\Loader(); $loader->registerDirs(array(FA_INCLUDE_MODULES . '/cli/tasks')); $loader->register(); // Create a console application $console = new \Phalcon\CLI\Console(); $console->setDI($di); // Process the console arguments $arguments = array(); foreach ($argv as $k => $arg) { if ($k == 1) { $task_parts = explode(':', $arg); $arguments['task'] = $task_parts[0]; $arguments['action'] = isset($task_parts[1]) ? $task_parts[1] : 'index'; } elseif ($k > 1) { $arguments['params'][] = $arg; } } // define global constants for the current task and action define('CURRENT_TASK', $arguments['task']); define('CURRENT_ACTION', $arguments['action']); try { // handle incoming arguments $console->handle($arguments); } catch (\Phalcon\Exception $e) { echo $e->getMessage(); exit(255); }
if (!extension_loaded('phalcon')) { throw new Exception("Phalcon extension is required"); } $params = $argv; $file = array_shift($argv); $task = array_shift($argv); define('APP_ROOT', __DIR__ . '/app'); $loader = new Phalcon\Loader(); $loader->registerNamespaces(['ApiDocs' => APP_ROOT]); $loader->register(); $di = new Phalcon\DI\FactoryDefault\CLI(); $di->setShared('dispatcher', function () use($di) { $dispatcher = new Phalcon\CLI\Dispatcher(); $dispatcher->setDI($di); $dispatcher->setDefaultNamespace('ApiDocs\\Tasks'); return $dispatcher; }); $di->setShared('modelsManager', function () { return new Phalcon\Mvc\Model\Manager(); }); $di->setShared('db', function () use($di) { $connection = new Phalcon\Db\Adapter\Pdo\Mysql((array) $di->get('config')->db); return $connection; }); $di->setShared('config', function () { return new Phalcon\Config(require APP_ROOT . '/config/config.php'); }); $console = new Phalcon\CLI\Console(); $console->setDI($di); $console->handle(['task' => $task ?: 'help', 'action' => 'main', 'params' => $argv]);
public function testIssue787() { $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->setShared('dispatcher', function () use($di) { $dispatcher = new Phalcon\CLI\Dispatcher(); $dispatcher->setDI($di); return $dispatcher; }); $console = new \Phalcon\CLI\Console(); $console->setDI($di); $console->handle(array('task' => 'issue787', 'action' => 'main')); $this->assertTrue(class_exists('Issue787Task')); $actual = Issue787Task::$output; $expected = "beforeExecuteRoute\ninitialize\n"; $this->assertEquals($actual, $expected); }
$di = new Phalcon\DI\FactoryDefault\CLI(); // Load the configuration file if (is_readable(APP_DIR . '/config/config_console.php')) { $config = (include APP_DIR . '/config/config_console.php'); } else { throw new Exception('Unable to load config_console.php'); } // Load the services file if (is_readable(APP_DIR . '/config/console_services.php')) { include APP_DIR . '/config/console_services.php'; } else { throw new Exception('Unable to load config_services.php'); } /** * Register the autoloader and tell it to register the tasks directory */ $loader = new Phalcon\Loader(); $loader->registerDirs([APP_DIR . '/libs/', APP_DIR . '/models/', APP_DIR . '/tasks/'])->register(); // Create a console application $console = new Phalcon\CLI\Console(); $console->setDI($di); // Define global constants for the current task and action define('CURRENT_TASK', $task['task']); define('CURRENT_ACTION', $task['action']); try { $console->handle($task); } catch (Exception $ex) { echo $ex->getMessage() . PHP_EOL; echo $ex->getTraceAsString() . PHP_EOL; exit; }
/** * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework */ $di = new \Phalcon\DI\FactoryDefault\CLI(); /** * Database connection is created based in the parameters defined in the configuration file */ $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)); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->set('modelsMetadata', function () use($config) { if (isset($config->models->metadata)) { $metaDataConfig = $config->models->metadata; $metadataAdapter = 'Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; return new $metadataAdapter(); } else { return new Phalcon\Mvc\Model\Metadata\Memory(); } }); $console = new \Phalcon\CLI\Console(); $console->setDI($di); // pass command-line arguments to handle $console->handle($_SERVER['argv']); } catch (Phalcon\Exception $e) { echo $e->getMessage(); } catch (PDOException $e) { echo $e->getMessage(); }
$dbWriteConfig = $currentConfig['write']['db']; $di->set($keyWrite, function () use($dbWriteConfig) { return new Phalcon\Db\Adapter\Pdo\Mysql($dbWriteConfig); }); $dbReadConfig = current($currentConfig['reads']); $di->set($keyRead, function () use($dbReadConfig) { return new Phalcon\Db\Adapter\Pdo\Mysql($dbReadConfig); }); } //设置默认数据库连接 $defaultDbKey = 'db' . ucfirst($config->balanceDb->default); $di->set('db', $di->get($defaultDbKey)); //Redis负载均衡 BalanceRedis::config($config->balanceRedis->toArray()); //URL 工具类 TUrl::config($config->url->toArray()); $console = new Phalcon\CLI\Console(); $console->setDI($di); //注册模块 $modules = array(); foreach ($config->modules as $key => $params) { if ($key == 'default') { continue; } $modules[$key] = array('className' => sprintf('Module\\%s\\Module', ucfirst($key)), 'path' => $params['path']); } $console->registerModules($modules); $console->handle($cliData); } catch (\Phalcon\Exception $e) { echo $e->getMessage(); }
$view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/../app/views/'); $view->registerEngines(array(".volt" => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(['compileAlways' => isset($_SERVER["APP_ENV"]) && $_SERVER["APP_ENV"] == 'dev', 'compiledPath' => function ($templatePath) { $templatePath = str_replace(__DIR__ . '/../app/views', __DIR__ . '/../app/views/cache', $templatePath); $dirName = dirname($templatePath); if (!is_dir($dirName)) { mkdir($dirName, 0755, true); } return $templatePath; }]); return $volt; })); return $view; }); if (PHP_SAPI != 'cli') { // URL Creation $url = str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']); //Handle the request $application = new \Phalcon\Mvc\Application(); $application->setDI($di); echo $application->handle($url)->getContent(); } else { $cli = new Phalcon\CLI\Console(); $cli->setDI($di); $cli->handle(array('task' => isset($argv[1]) ? $argv[1] : null, 'action' => isset($argv[2]) ? $argv[2] : 'index', 'params' => count($argv) > 3 ? array_slice($argv, 3) : [])); } } catch (\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
<?php //Using the CLI factory default services container $di = new Phalcon\DI\FactoryDefault\CLI(); //Create a console application $console = new \Phalcon\CLI\Console(); $console->setDI($di); // $console->handle(array('shell_script_name', 'echo'));