Skip to content

Commit

Permalink
#2440 - Partial implementation of new console commands with 'phalcon/…
Browse files Browse the repository at this point in the history
…cli-options-parser' package
  • Loading branch information
Jeckerson committed Jan 9, 2025
1 parent 8ad3733 commit c79b710
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ext-xml": "*",
"ext-zlib": "*",
"monolog/monolog": "^2.3",
"phalcon/cli-options-parser": "^2.0",
"symfony/console": "^5.2",
"symfony/event-dispatcher": "^6.0"
},
Expand Down
82 changes: 81 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions src/Console/FullCleanCommand.php
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()
)
);
}
}
}
26 changes: 26 additions & 0 deletions src/Console/HelpCommand.php
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;
}
}
143 changes: 143 additions & 0 deletions zephir-new
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();

0 comments on commit c79b710

Please sign in to comment.