Source of file More.php
Size: 4,303 Bytes - Last Modified: 2021-12-23T10:34:07+00:00
/var/www/docs.ssmods.com/process/src/code/core/More.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 | <?php namespace SilverStripe\Sakemore\Core; use Controller; use ReflectionMethod; /** * This class defines the base 'more' URL used with sake. */ class More extends Controller { /** * All sake more commands are prefixed with the 'more' URL. */ private static $url_handlers = array( '' => 'init', ); private static $allowed_actions = array( 'init', ); /** * Prepare to run a sake more command. */ public function init() { // Ensure it's being run via the command line. if (!$this->isCli()) { // If it's not, redirect to the homepage. $this->redirect('/'); return parent::init(); } // What're the command and arguments? if (!array_key_exists('args', $_GET)) { $cmdargs = array(); } else { $cmdargs = $_GET['args']; } // Validate them. if (empty($cmdargs)) { return $this->showError( 'Expected parameter. Type "sake more help" for a list of commands' ); } // Get all the potential commands. $allcommands = $this->availableCommands(); // Which command is being called? $cmd = array_shift($cmdargs); if (!array_key_exists($cmd, $allcommands)) { return $this->showError( "Unable to find command '$cmd'. Run 'sake more help' for list of commands" ); } // Run the given command. if (count($allcommands[$cmd]) == 2) { list($class, $function) = $allcommands[$cmd]; $argarray = false; } else { list($class, $function, $argarray) = $allcommands[$cmd]; } // Passing the arguments as one array or individual arguments? if ($argarray) { // One single array? Useful for when using unlimited arguments. $cmdargs = array($cmdargs); } $object = null; if (is_object($class)) { // Passed an object instead of a static class? Ok. $object = $class; $class = get_class($object); } $method = new ReflectionMethod($class, $function); $response = $method->invokeArgs($object, $cmdargs); // Print the response. if (!is_null($response)) { printf("%s\n", $response); } // Done. die(); } /** * Determines if this script is being run via the command line. */ protected function isCli() { // Prepare variables. $sapi = php_sapi_name(); // Done. return $sapi === 'cli'; } /** * Gets a list of available commands. */ protected function availableCommands() { /* * key - This is unique to each command. * - The command - 'sake more [command] [args]' * value is an array * 1. $this - The object to call or null for a static call. * 2. method name - The function to call on that object/class. * 3. (Optional) - settings - array. * - 'args' => 'many' (default) or 'single' - Are all arguments passed as a single array or individual function arguments? */ // Prepare variables. $list = array( 'help' => array($this, 'getHelp'), ); // Add the others to the list. $this->extend('commands', $list); // Done. return $list; } /** * Displays errors. */ protected function showError($msg) { // @todo Add arguments and translation. printf("%s\n", $msg); die(); } /** * Display available help information. */ public function getHelp($item = null) { // Prepare variables. $briefs = array(); $parameters = array(); $examples = array(); // Get help details. $this->extend('help_brief', $briefs); $this->extend('help_parameters', $parameters); $this->extend('help_examples', $examples); $commands = array_unique(array_merge( array_keys($briefs), array_keys($parameters), array_keys($examples) )); // Filter this to a single command? if (!is_null($item) && in_array($item, $commands)) { $commands = array($item); } // Display the data. echo "Help details for sake:\n"; foreach ($commands as $command) { echo "\n"; if (array_key_exists($command, $briefs)) { printf("%s - %s\n", $command, $briefs[$command]); } if (array_key_exists($command, $parameters)) { $pre = 'Arguments:'; foreach ($parameters[$command] as $parameter) { printf("%s %s\n", $pre, $parameter); $pre = ' '; } } if (array_key_exists($command, $examples)) { $pre = 'Example:'; foreach ($examples[$command] as $example) { printf("%s sake more %s\n", $pre, $example); $pre = ' '; } } } } } |