| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of FSConsoleTools |
||
| 4 | * Copyright (C) 2018 Francesc Pineda Segarra <[email protected]> |
||
| 5 | * |
||
| 6 | * This program is free software: you can redistribute it and/or modify |
||
| 7 | * it under the terms of the GNU Lesser General Public License as |
||
| 8 | * published by the Free Software Foundation, either version 3 of the |
||
| 9 | * License, or (at your option) any later version. |
||
| 10 | * |
||
| 11 | * This program is distributed in the hope that it will be useful, |
||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | * GNU Lesser General Public License for more details. |
||
| 15 | * |
||
| 16 | * You should have received a copy of the GNU Lesser General Public License |
||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 18 | */ |
||
| 19 | |||
| 20 | namespace FacturaScriptsUtils\Console; |
||
| 21 | |||
| 22 | use FacturaScripts\Core\Base\DataBase; |
||
|
0 ignored issues
–
show
|
|||
| 23 | |||
| 24 | const DS = DIRECTORY_SEPARATOR; |
||
| 25 | |||
| 26 | if (!\defined('FS_FOLDER')) { |
||
| 27 | \define('FS_FOLDER', __DIR__ . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (!\defined('FS_DEBUG')) { |
||
| 31 | \define('FS_DEBUG', true); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Class ConsoleAbstract |
||
| 36 | * |
||
| 37 | * @author Francesc Pineda Segarra <[email protected]> |
||
| 38 | */ |
||
| 39 | abstract class ConsoleAbstract |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * Arguments received from command execution. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $argv; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * DataBase object. |
||
| 50 | * |
||
| 51 | * @var DataBase |
||
| 52 | */ |
||
| 53 | protected $dataBase; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Developer name. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $devName; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Developer email. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $devMail; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Auto reply it enabled. |
||
| 71 | * |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | public $autoReply = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Auto hide messages if enabled. |
||
| 78 | * |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | public $autoHide = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Start point to run the command. |
||
| 85 | * |
||
| 86 | * @param array $params |
||
| 87 | * |
||
| 88 | * @return int |
||
| 89 | */ |
||
| 90 | abstract public function run(...$params): int; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return description about this class. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | abstract public function getDescription(): string; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Print help information to the user. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | abstract public function getHelpMsg(): string; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * ConsoleAbstract constructor. |
||
| 108 | */ |
||
| 109 | public function __construct() |
||
| 110 | { |
||
| 111 | $this->setDevName('YOUR NAME'); |
||
| 112 | $this->setDevMail('YOUR@EMAIL'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Initialize. |
||
| 117 | */ |
||
| 118 | public function init() |
||
| 119 | { |
||
| 120 | if (\DB_CONNECTION) { |
||
| 121 | if (!isset($this->dataBase)) { |
||
| 122 | $this->dataBase = new DataBase(); |
||
| 123 | $this->dataBase->connect(); |
||
| 124 | } |
||
| 125 | } else { |
||
| 126 | trigger_error('A database connection is needed. Do you set your config.php file?'); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Terminate. |
||
| 132 | */ |
||
| 133 | public function terminate() |
||
| 134 | { |
||
| 135 | $this->dataBase->close(); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns an associative array of available methods for the user. |
||
| 140 | * Add more options if you want to add support for custom methods. |
||
| 141 | * [ |
||
| 142 | * '-h' => 'getHelpMsg', |
||
| 143 | * '--help' => 'getHelpMsg', |
||
| 144 | * ] |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function getUserMethods(): array |
||
| 149 | { |
||
| 150 | return [ |
||
| 151 | '-h' => 'getHelpMsg', |
||
| 152 | '--help' => 'getHelpMsg', |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Ask user to continue and return a boolean. |
||
| 158 | * |
||
| 159 | * @param bool $autoReply |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function areYouSure($autoReply = false) |
||
| 164 | { |
||
| 165 | $this->showMessage(\PHP_EOL . 'Are you sure? [y/n] '); |
||
| 166 | $stdin = $autoReply ? $autoReply : trim(fgets(STDIN)); |
||
| 167 | switch ($stdin) { |
||
| 168 | case 'y': |
||
| 169 | case 'Y': |
||
| 170 | case 'yes': |
||
| 171 | case 'Yes': |
||
| 172 | case 'true': |
||
| 173 | case true: |
||
|
0 ignored issues
–
show
|
|||
| 174 | return true; |
||
| 175 | default: |
||
| 176 | return $autoReply; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Set database. |
||
| 182 | * |
||
| 183 | * @param DataBase\Mysql|DataBase\Postgresql $dataBase |
||
|
0 ignored issues
–
show
The type
FacturaScripts\Core\Base\DataBase\Postgresql was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
The type
FacturaScripts\Core\Base\DataBase\Mysql was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 184 | */ |
||
| 185 | public function setDataBase($dataBase) |
||
| 186 | { |
||
| 187 | $this->dataBase = $dataBase; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Return the DataBase object. |
||
| 192 | * |
||
| 193 | * @return DataBase |
||
| 194 | */ |
||
| 195 | public function getDataBase() |
||
| 196 | { |
||
| 197 | return $this->dataBase; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns developer name. |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getDevName(): string |
||
| 206 | { |
||
| 207 | return $this->devName; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Sets developer name. |
||
| 212 | * |
||
| 213 | * @param string $devName |
||
| 214 | */ |
||
| 215 | public function setDevName(string $devName) |
||
| 216 | { |
||
| 217 | $this->devName = $devName; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns developer email. |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getDevMail(): string |
||
| 226 | { |
||
| 227 | return $this->devMail; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Sets developer email |
||
| 232 | * |
||
| 233 | * @param string $devMail |
||
| 234 | */ |
||
| 235 | public function setDevMail(string $devMail) |
||
| 236 | { |
||
| 237 | $this->devMail = $devMail; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Save file. |
||
| 242 | * |
||
| 243 | * @param string $pathName |
||
| 244 | * @param string $content |
||
| 245 | * |
||
| 246 | * @return bool|int |
||
| 247 | */ |
||
| 248 | protected function saveFile(string $pathName, string $content) |
||
| 249 | { |
||
| 250 | return file_put_contents($pathName, $content); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Print a message. |
||
| 255 | * |
||
| 256 | * @param string $msg |
||
| 257 | */ |
||
| 258 | protected function showMessage($msg) |
||
| 259 | { |
||
| 260 | if ($this->autoHide) { |
||
| 261 | trigger_error($msg); |
||
| 262 | } else { |
||
| 263 | echo $msg; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths