Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8d121b55e2
Fetching contributors…

Cannot retrieve contributors at this time

executable file 144 lines (128 sloc) 3.975 kb
#!/usr/bin/env php
<?php
/**
* Console tool for invoking mwop.net scripts
*
* Usage:
*
* console [--global-options] Module:script [--script-options]
*
* where "Module" is a module name, and "script" is a script found in that
* module's "bin" directory, minus the ".php" suffix. Example:
*
* console Blog:post -c config.file -p post.file
*
* It then executes the requested script in the given environment, and within
* the same execution session.
*/
use Zend\Mvc\Service;
use Zend\ServiceManager\ServiceManager;
chdir(__DIR__);
ini_set("display_errors", true);
error_reporting(E_ALL | E_STRICT);
define('MWOP_CONSOLE', true);
// Setup autoloading
include_once 'vendor/autoload.php';
// Setup getopt rules
$rules = array(
'help|h' => 'Console usage message (this message)',
);
if (2 > $argc) {
$message = "Too few arguments.\n";
try {
$opt = new Zend\Console\Getopt($rules);
$message .= $opt->getUsageMessage();
} catch (Zend\Console\Exception $e) {
$message .= $e->getUsageMessage();
}
usage($message, 1);
}
$consoleArgs = array();
$breakFound = false;
$scriptFound = false;
for ($i = 1; $i <= $argc; $i++) {
if (!isset($argv[$i])) {
break;
}
$value = $argv[$i];
if ('--' == $value) {
$breakFound = true;
break;
}
if (strstr($value, ':')) {
$scriptFound = true;
break;
}
}
if (!$breakFound && !$scriptFound) {
$message = "No script argument provided.\n";
try {
$opt = new Zend\Console\Getopt($rules);
$message .= $opt->getUsageMessage();
} catch (Zend\Console\Exception $e) {
$message .= $e->getUsageMessage();
}
usage($message, 1);
}
$consoleArgs = array_slice($argv, 1, $i - 1);
$scriptArgs = array_slice($argv, $i);
try {
$opt = new Zend\Console\Getopt($rules, $consoleArgs);
} catch (Zend\Console\Exception $e) {
$message = "Error parsing arguments.\n";
$message .= $e->getUsageMessage();
usage($message, 1);
}
if ($opt->getOption('h')) {
usage($opt->getUsageMessage(), 1);
}
if ($breakFound) {
array_shift($scriptArgs);
$keys = array_keys($scriptArgs);
$firstKey = array_shift($keys);
$first = $scriptArgs[$firstKey];
if (!strstr($first, ':')) {
$message = "Missing command argument\n";
$message .= $opt->getUsageMessage();
usage($message, 1);
}
unset($keys, $firstKey, $first);
}
$scriptArg = array_shift($scriptArgs);
list($module, $script) = explode(':', $scriptArg, 2);
// Initialize application
// Need to allow overriding services so we can override the various places the
// console substitutes itself
$config = include 'config/application.config.php';
$smConfig = isset($config['service_manager']) ? $config['service_manager'] : array();
$services = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
$services->setService('ApplicationConfig', $config);
$services->setAllowOverride(true);
$moduleManager = $services->get('ModuleManager');
$moduleManager->loadModules();
$application = $services->get('Application')->bootstrap();
$services->setAllowOverride(false);
// Determine what modules are present
$loadedModules = $moduleManager->getLoadedModules();
if (!isset($loadedModules[$module])) {
usage("Module '$module' not found; cannot execute script\n", 1);
}
$moduleClass = $loadedModules[$module];
$r = new ReflectionObject($moduleClass);
$scriptPath = dirname($r->getFileName()) . '/bin/' . $script . '.php';
$argv = $scriptArgs;
// Cleanup before execution: unset variables
unset(
$rules, $opt, $consoleArgs, $breakFound, $scriptFound, $i, $value,
$scriptArgs, $module, $script, $appConfig, $loadedModules,
$moduleClass, $r
);
include $scriptPath;
exit(0);
function usage($message, $exit = 1)
{
echo "Console tool:\n";
echo "console [options] Module:script [options]\n\n";
echo $message;
exit($exit);
}
Jump to Line
Something went wrong with that request. Please try again.