|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Litipk\JupyterPhpInstaller\Console; |
| 5 | + |
| 6 | + |
| 7 | +use Litipk\JupyterPhpInstaller\Command\InfoCommand; |
| 8 | +use Litipk\JupyterPhpInstaller\Command\InstallCommand; |
| 9 | +use Litipk\JupyterPhpInstaller\Command\UninstallCommand; |
| 10 | +use Litipk\JupyterPhpInstaller\Command\UpdateCommand; |
| 11 | +use Litipk\JupyterPhpInstaller\IO\ConsoleIO; |
| 12 | +use Litipk\JupyterPhpInstaller\IO\IOInterface; |
| 13 | +use Litipk\JupyterPhpInstaller\Util\ErrorHandler; |
| 14 | + |
| 15 | +use Symfony\Component\Console\Application as BaseApplication; |
| 16 | +use Symfony\Component\Console\Formatter\OutputFormatter; |
| 17 | +use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * The console application that handles the commands |
| 25 | + */ |
| 26 | +final class Application extends BaseApplication |
| 27 | +{ |
| 28 | + /** @var IOInterface */ |
| 29 | + protected $io; |
| 30 | + |
| 31 | + public function __construct() |
| 32 | + { |
| 33 | + static $shutdownRegistered = false; |
| 34 | + |
| 35 | + if (function_exists('ini_set') && extension_loaded('xdebug')) { |
| 36 | + ini_set('xdebug.show_exception_trace', false); |
| 37 | + ini_set('xdebug.scream', false); |
| 38 | + } |
| 39 | + |
| 40 | + if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) { |
| 41 | + date_default_timezone_set(@date_default_timezone_get()); |
| 42 | + } |
| 43 | + |
| 44 | + if (!$shutdownRegistered) { |
| 45 | + $shutdownRegistered = true; |
| 46 | + |
| 47 | + register_shutdown_function(function () { $this->handleShutdown(); }); |
| 48 | + } |
| 49 | + |
| 50 | + parent::__construct('Jupyter-PHP Installer', '0.1'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Starts the application, boilerplate code. |
| 55 | + * @param InputInterface|null $input |
| 56 | + * @param OutputInterface|null $output |
| 57 | + * @return int |
| 58 | + */ |
| 59 | + public function run(InputInterface $input = null, OutputInterface $output = null) |
| 60 | + { |
| 61 | + if (null === $output) { |
| 62 | + $output = new ConsoleOutput( |
| 63 | + ConsoleOutput::VERBOSITY_NORMAL, |
| 64 | + null, |
| 65 | + new OutputFormatter(false, [ |
| 66 | + 'highlight' => new OutputFormatterStyle('red'), |
| 67 | + 'warning' => new OutputFormatterStyle('black', 'yellow'), |
| 68 | + ]) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return parent::run($input, $output); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Runs the application's business logic. |
| 77 | + */ |
| 78 | + public function doRun(InputInterface $input, OutputInterface $output) |
| 79 | + { |
| 80 | + $this->io = new ConsoleIO($input, $output, $this->getHelperSet()); |
| 81 | + ErrorHandler::register($this->io); |
| 82 | + $io = $this->io; |
| 83 | + |
| 84 | + if (PHP_VERSION_ID < 50509) { |
| 85 | + $io->writeError( |
| 86 | + '<warning>'. |
| 87 | + 'This installer only officially supports PHP 5.5.9 and above, you will most likely encounter problems with your PHP '. |
| 88 | + PHP_VERSION.', upgrading is strongly recommended'. |
| 89 | + '</warning>' |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if (extension_loaded('xdebug')) { |
| 94 | + $io->writeError( |
| 95 | + '<warning>'. |
| 96 | + 'You are running PHP-CLI with xdebug enabled. This will have a major impact on the kernel\'s performance.'. |
| 97 | + '</warning>' |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return parent::doRun($input, $output); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Initializes all the composer commands. |
| 106 | + */ |
| 107 | + protected function getDefaultCommands() |
| 108 | + { |
| 109 | + $commands = parent::getDefaultCommands(); |
| 110 | + $commands[] = new InstallCommand(); |
| 111 | + $commands[] = new UninstallCommand(); |
| 112 | + $commands[] = new UpdateCommand(); |
| 113 | + $commands[] = new InfoCommand(); |
| 114 | + |
| 115 | + |
| 116 | + if ('phar:' === substr(__FILE__, 0, 5)) { |
| 117 | + // Nothing to do, yet |
| 118 | + } |
| 119 | + |
| 120 | + return $commands; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Handles the program shutdown. |
| 125 | + */ |
| 126 | + private function handleShutdown() |
| 127 | + { |
| 128 | + $lastError = error_get_last(); |
| 129 | + |
| 130 | + if (!empty($lastError) && isset($lastError['message'])) { |
| 131 | + $this->handleShutdownLastError($lastError); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Handles the errors that caused the shutdown. |
| 137 | + * @param array $lastError |
| 138 | + */ |
| 139 | + private function handleShutdownLastError(array $lastError) |
| 140 | + { |
| 141 | + if ( |
| 142 | + strpos($lastError['message'], 'Allowed memory') !== false /* Zend PHP out of memory error */ || |
| 143 | + strpos($lastError['message'], 'exceeded memory') !== false /* HHVM out of memory errors */ |
| 144 | + ) { |
| 145 | + $this->explainMemoryErrorSolution(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Explains to the user what can be done to solve the memory error that caused the shutdown. |
| 151 | + */ |
| 152 | + private function explainMemoryErrorSolution() |
| 153 | + { |
| 154 | + if (extension_loaded('xdebug')) { |
| 155 | + echo "\n" . 'You should disable the Xdebug extension in your php.ini settings file.' . "\n"; |
| 156 | + } else { |
| 157 | + echo "\n" . 'You should increase the `memory_limit` parameter in your php.ini settings file.' . "\n"; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments