From 4da0d68bada69f8339a94d7ddc8d51a75e5a4d6f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 4 May 2018 08:16:03 -0400 Subject: [PATCH] Add console CLI command - Fix App to be used in CLI mode --- bin/console | 10 ++++ bin/console.bat | 4 ++ bin/console.php | 6 +++ src/App.php | 17 ++++--- src/Core/Console.php | 106 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 bin/console create mode 100644 bin/console.bat create mode 100644 bin/console.php create mode 100644 src/Core/Console.php diff --git a/bin/console b/bin/console new file mode 100644 index 00000000..4d76bdc4 --- /dev/null +++ b/bin/console @@ -0,0 +1,10 @@ +#!/bin/bash + +dir=$(cd "${0%[/\\]*}" > /dev/null; pwd) + +if [[ -d /proc/cygdrive && $(which php) == $(readlink -n /proc/cygdrive)/* ]]; then + # We are in Cgywin using Windows php, so the path must be translated + dir=$(cygpath -m "$dir"); +fi + +php "${dir}/console.php" "$@" diff --git a/bin/console.bat b/bin/console.bat new file mode 100644 index 00000000..06c41a03 --- /dev/null +++ b/bin/console.bat @@ -0,0 +1,4 @@ +@echo OFF +:: in case DelayedExpansion is on and a path contains ! +setlocal DISABLEDELAYEDEXPANSION +php "%~dp0console.php" %* diff --git a/bin/console.php b/bin/console.php new file mode 100644 index 00000000..07f26bbf --- /dev/null +++ b/bin/console.php @@ -0,0 +1,6 @@ +#!/usr/bin/env php +execute(); diff --git a/src/App.php b/src/App.php index 8540892a..5ce9fbc6 100644 --- a/src/App.php +++ b/src/App.php @@ -34,17 +34,20 @@ class App $this->pager = array(); $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http'); - $this->hostname = str_replace('www.', '', $_SERVER['SERVER_NAME']); + if (!empty($_SERVER['SERVER_NAME'])) { + $this->hostname = str_replace('www.', '', $_SERVER['SERVER_NAME']); + } set_include_path(get_include_path() - . PATH_SEPARATOR . "include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.'); - if (substr($_SERVER['QUERY_STRING'], 0, 2) == "q=") { - $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 2); - } + if (!empty($_SERVER['QUERY_STRING'])) { + if (substr($_SERVER['QUERY_STRING'], 0, 2) == "q=") { + $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 2); + } - $this->query_string = $_SERVER['QUERY_STRING']; + $this->query_string = $_SERVER['QUERY_STRING']; + } $q = isset($_GET['q']) ? $_GET['q'] : ''; $this->cmd = trim($q, '/'); @@ -120,4 +123,4 @@ class App '$baseurl' => $this->get_baseurl() )); } -} \ No newline at end of file +} diff --git a/src/Core/Console.php b/src/Core/Console.php new file mode 100644 index 00000000..82473009 --- /dev/null +++ b/src/Core/Console.php @@ -0,0 +1,106 @@ + + */ +class Console extends \Asika\SimpleConsole\Console +{ + // Disables the default help handling + protected $helpOptions = []; + protected $customHelpOptions = ['h', 'help', '?']; + + protected $subConsoles = [ + 'config' => __NAMESPACE__ . '\Console\Config', + 'probe' => __NAMESPACE__ . '\Console\Probe', + 'po2php' => __NAMESPACE__ . '\Console\PoToPhp', + ]; + + protected function getHelp() + { + $help = << [] [-v] + +Commands: + config Edit site config + probe Probe a single site + po2php Generate a strings.php file from a messages.po file + +Options: + -h|--help|-? Show help information + -v Show more debug information. +HELP; + return $help; + } + + protected function doExecute() + { + if ($this->getOption('v')) { + $this->out('Executable: ' . $this->executable); + $this->out('Arguments: ' . var_export($this->args, true)); + $this->out('Options: ' . var_export($this->options, true)); + } + + $showHelp = false; + $subHelp = false; + $command = null; + + if ($this->getOption('version')) { + $this->out('Friendica Console version ' . BUILD_ID); + + return 0; + } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0 + ) { + $showHelp = true; + } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') { + $command = $this->getArgument(1); + $subHelp = true; + array_shift($this->args); + array_shift($this->args); + } elseif (count($this->args) >= 1) { + $command = $this->getArgument(0); + array_shift($this->args); + } + + if (is_null($command)) { + $this->out($this->getHelp()); + return 0; + } + + $console = $this->getSubConsole($command); + + if ($subHelp) { + $console->setOption($this->customHelpOptions, true); + } + + return $console->execute(); + } + + private function getSubConsole($command) + { + if ($this->getOption('v')) { + $this->out('Command: ' . $command); + } + + if (!isset($this->subConsoles[$command])) { + throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist'); + } + + $subargs = $this->args; + array_unshift($subargs, $this->executable); + + $className = $this->subConsoles[$command]; + + $subconsole = new $className($subargs); + + foreach ($this->options as $name => $value) { + $subconsole->setOption($name, $value); + } + + return $subconsole; + } + +}