-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2440 - Partial implementation of new console commands with 'phalcon/…
…cli-options-parser' package
- Loading branch information
Showing
5 changed files
with
298 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Zephir. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zephir\Console; | ||
|
||
use Zephir\Exception\FileSystemException; | ||
use Zephir\Os; | ||
|
||
final class FullCleanCommand | ||
{ | ||
public static function execute(): void | ||
{ | ||
/** | ||
* TODO: Do we need a batch file for Windows like "clean" as used below? | ||
* TODO: The 'clean' file contains duplicated commands | ||
*/ | ||
try { | ||
if (Os::isWindows()) { | ||
system('cd ext && phpize --clean'); | ||
} else { | ||
system('cd ext && phpize --clean > /dev/null'); | ||
system('cd ext && ./clean > /dev/null'); | ||
} | ||
} catch (FileSystemException $e) { | ||
fwrite( | ||
STDERR, | ||
sprintf( | ||
"For reasons beyond Zephir's control, a filesystem error has occurred. " . | ||
'Please note: On Linux/Unix systems the current user must have the delete and execute ' . | ||
'permissions on the internal cache directory, For more information see chmod(1) and chown(1). ' . | ||
'System error was: %s', | ||
$e->getMessage() | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Zephir. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zephir\Console; | ||
|
||
use Zephir\Zephir; | ||
|
||
final class HelpCommand | ||
{ | ||
public static function execute(): void | ||
{ | ||
$version = explode('-', Zephir::VERSION); | ||
|
||
echo Zephir::LOGO . sprintf('Zephir %s by the Phalcon Team', $version[0]) . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
/** | ||
* This file is part of the Zephir. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
use Phalcon\Cop\Parser as CliParser; | ||
use Zephir\Backend\Backend; | ||
use Zephir\Compiler; | ||
use Zephir\Config; | ||
use Zephir\Console\Application; | ||
use Zephir\Console\Command\ApiCommand; | ||
use Zephir\Console\Command\BuildCommand; | ||
use Zephir\Console\Command\CleanCommand; | ||
use Zephir\Console\Command\CompileCommand; | ||
use Zephir\Console\Command\GenerateCommand; | ||
use Zephir\Console\Command\InitCommand; | ||
use Zephir\Console\Command\InstallCommand; | ||
use Zephir\Console\Command\ListCommand; | ||
use Zephir\Console\Command\StubsCommand; | ||
use Zephir\Console\FullCleanCommand; | ||
use Zephir\FileSystem\HardDisk; | ||
use Zephir\Logger\Formatter\CompilerFormatter; | ||
use Zephir\Parser\Manager; | ||
use Zephir\Parser\Parser; | ||
|
||
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') { | ||
fprintf( | ||
STDERR, | ||
'Zephir should be invoked via the CLI version of PHP, not the %s SAPI.'.PHP_EOL, | ||
PHP_SAPI | ||
); | ||
|
||
exit(1); | ||
} | ||
|
||
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) { | ||
if (file_exists($file)) { | ||
include_once $file; | ||
break; | ||
} | ||
} | ||
|
||
if (!class_exists('Composer\Autoload\ClassLoader', false)) { | ||
fwrite(STDERR, 'Unable to find the Composer autoloader.'.PHP_EOL); | ||
|
||
exit(1); | ||
} | ||
|
||
set_error_handler(static function ($code, $message, $file = '', $line = -1) { | ||
if (error_reporting() & $code) { | ||
throw new ErrorException($message, 0, $code, (string) $file, $line); | ||
} | ||
}); | ||
|
||
if (filter_var(getenv('ZEPHIR_DEBUG'), FILTER_VALIDATE_BOOLEAN)) { | ||
set_exception_handler(static function (Throwable $t) { | ||
fwrite(STDERR, "[ERROR] {$t->getMessage()}". PHP_EOL); | ||
|
||
exit(1); | ||
}); | ||
} | ||
|
||
/** | ||
* When it is executed inside .phar, realpath() will return `false`. | ||
*/ | ||
$rootPath = Phar::running() ?: realpath(dirname(__FILE__)); | ||
$config = Config::fromServer(); | ||
|
||
/** | ||
* Logger | ||
*/ | ||
$formatter = new CompilerFormatter($config); | ||
|
||
$consoleStdErrorHandler = new StreamHandler('php://stderr', Logger::WARNING, false); | ||
$consoleStdErrorHandler->setFormatter($formatter); | ||
|
||
$consoleStdOutHandler = new StreamHandler('php://stdout', Logger::INFO, false); | ||
$consoleStdOutHandler->setFormatter($formatter); | ||
|
||
$disk = new HardDisk(getcwd().'/.zephir'); | ||
|
||
$parser = new Parser(); | ||
$logger = new Logger('zephir', [ | ||
$consoleStdErrorHandler, | ||
$consoleStdOutHandler, | ||
]); | ||
$compilerFactory = new Compiler\CompilerFileFactory($config, $disk, $logger); | ||
$backend = new Backend($config, $rootPath.'/kernel', $rootPath.'/templates'); | ||
|
||
$compiler = new Compiler($config, $backend, new Manager($parser), $disk, $compilerFactory); | ||
$compiler->setPrototypesPath($rootPath.'/prototypes'); | ||
$compiler->setOptimizersPath($rootPath.'/src/Optimizers'); | ||
$compiler->setTemplatesPath($rootPath.'/templates'); | ||
$compiler->setLogger($logger); | ||
|
||
$cli = new CliParser(); | ||
$params = $cli->parse(); | ||
|
||
$showHelp = isset($params['help']) || isset($params['h']); | ||
$command = $params[0] ?? 'help'; | ||
switch ($command) { | ||
case 'api': | ||
// TODO | ||
break; | ||
|
||
case 'build': | ||
// TODO | ||
break; | ||
|
||
case 'fullclean': | ||
FullCleanCommand::execute(); | ||
break; | ||
case 'help': | ||
default: | ||
\Zephir\Console\HelpCommand::execute(); | ||
break; | ||
} | ||
|
||
return; | ||
|
||
$application = new Application(); | ||
$application->add(new ApiCommand($compiler, $config)); | ||
$application->add(new BuildCommand()); | ||
$application->add(new CleanCommand($disk)); | ||
$application->add(new CompileCommand($compiler)); | ||
$application->add(new GenerateCommand($compiler)); | ||
$application->add(new InitCommand($backend, $config, $logger)); | ||
$application->add(new InstallCommand($compiler, $config)); | ||
$application->add(new ListCommand()); | ||
$application->add(new StubsCommand($compiler)); | ||
|
||
$application->run(); |