Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions src/App/Traits/ConsoleAppTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
namespace Quantum\App\Traits;

use Symfony\Component\Console\Application;
use Quantum\Console\QtCommand;
use Quantum\Console\CommandDiscovery;
use ReflectionException;
use ReflectionClass;
use Exception;

/**
Expand Down Expand Up @@ -64,32 +63,18 @@ private function registerAppCommands()
/**
* @param string $directory
* @param string $namespace
* @return void
* @throws ReflectionException
*/
private function registerCommands(string $directory, string $namespace)
{
foreach (get_directory_classes($directory) as $className) {
$commandClass = $namespace . $className;

if (!class_exists($commandClass)) {
continue;
}

$commandReflection = new ReflectionClass($commandClass);
$commands = CommandDiscovery::discover($directory, $namespace);

if (!$commandReflection->isInstantiable()) {
continue;
}

if ($commandReflection->isSubclassOf(QtCommand::class)) {
$this->application->add($commandReflection->newInstance());
}
foreach ($commands as $command) {
$this->application->add(new $command['class']());
}
}

/**
* @return void
* @throws Exception
*/
private function validateCommand(): void
Expand Down
62 changes: 62 additions & 0 deletions src/Console/CommandDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.9
*/

namespace Quantum\Console;

use ReflectionException;
use ReflectionClass;

/**
* Class CommandDiscovery
* @package Quantum\Console
*/
class CommandDiscovery
{

/**
* @param string $directory
* @param string $namespace
* @return array
* @throws ReflectionException
*/
public static function discover(string $directory, string $namespace): array
{
$commands = [];

foreach (get_directory_classes($directory) as $className) {
$commandClass = $namespace . $className;

if (!class_exists($commandClass)) {
continue;
}

$commandReflection = new ReflectionClass($commandClass);

if (!$commandReflection->isInstantiable() || !$commandReflection->isSubclassOf(QtCommand::class)) {
continue;
}

$instance = $commandReflection->newInstance();

$commands[] = [
'class' => $commandClass,
'name' => $instance->getName(),
'description' => $instance->getDescription(),
'help' => $instance->getHelp(),
];
}

return $commands;
}
}
8 changes: 7 additions & 1 deletion src/Console/Commands/EnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.7
* @since 2.9.9
*/

namespace Quantum\Console\Commands;

use Quantum\Libraries\Storage\Factories\FileSystemFactory;
use Quantum\Config\Exceptions\ConfigException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Console\QtCommand;
use ReflectionException;
use Quantum\App\App;

/**
Expand Down Expand Up @@ -55,6 +58,9 @@ class EnvCommand extends QtCommand
/**
* Executes the command and creates new .env file
* @throws BaseException
* @throws ConfigException
* @throws DiException
* @throws ReflectionException
*/
public function exec()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/InstallToolkitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.8
* @since 2.9.9
*/

namespace Quantum\Console\Commands;

use Quantum\Environment\Exceptions\EnvException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Quantum\Environment\Exceptions\EnvException;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Quantum\Environment\Environment;
Expand Down
64 changes: 30 additions & 34 deletions src/Console/QtCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.8.0
* @since 2.9.9
*/

namespace Quantum\Console;
Expand Down Expand Up @@ -83,33 +83,12 @@ public function __construct()
$this->setHelp($this->help);
}

/**
* Configures the current command.
*/
protected function configure()
{
$this->setArguments();
$this->setOptions();
}

/**
* Executes the current command.
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->exec();
}

/**
* Returns the argument value for a given argument name.
* @param string|null $key
* @return mixed|string
*/
protected function getArgument(string $key = null)
public function getArgument(string $key = null)
{
return $this->input->getArgument($key) ?? '';
}
Expand All @@ -119,7 +98,7 @@ protected function getArgument(string $key = null)
* @param string|null $key
* @return mixed|string
*/
protected function getOption(string $key = null)
public function getOption(string $key = null)
{
return $this->input->getOption($key) ?? '';
}
Expand All @@ -137,7 +116,7 @@ public function output(string $message)
* Outputs the string to console as info
* @param string $message
*/
protected function info(string $message)
public function info(string $message)
{
$this->output->writeln("<info>$message</info>");
}
Expand All @@ -146,7 +125,7 @@ protected function info(string $message)
* Outputs the string to console as comment
* @param string $message
*/
protected function comment(string $message)
public function comment(string $message)
{
$this->output->writeln("<comment>$message</comment>");
}
Expand All @@ -155,7 +134,7 @@ protected function comment(string $message)
* Outputs the string to console as question
* @param string $message
*/
protected function question(string $message)
public function question(string $message)
{
$this->output->writeln("<question>$message</question>");
}
Expand All @@ -168,24 +147,42 @@ protected function question(string $message)
public function confirm(string $message): bool
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion($message . ' [y/N] ', false);

if ($helper->ask($this->input, $this->output, $question)) {
return true;
}
$question = new ConfirmationQuestion($message . ' [y/N] ', false);

return false;
return (bool) $helper->ask($this->input, $this->output, $question);
}

/**
* Outputs the string to console as error
* @param string $message
*/
protected function error(string $message)
public function error(string $message)
{
$this->output->writeln("<error>$message</error>");
}

/**
* Configures the current command.
*/
protected function configure()
{
$this->setArguments();
$this->setOptions();
}

/**
* Executes the current command.
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->exec();
}

/**
* Sets command arguments
*/
Expand Down Expand Up @@ -228,5 +225,4 @@ private function setOptions()
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace {{MODULE_NAMESPACE}}\Controllers;

use {{MODULE_NAMESPACE}}\Services\CommandService;
use Quantum\Http\Response;

/**
Expand Down Expand Up @@ -51,6 +52,8 @@ class PageController extends BaseController
'title' => t('common.about') . ' | ' . config()->get('app.name'),
]);

$response->html($this->view->render('pages/about'));
$commands = service(CommandService::class)->getAllCommands();

$response->html($this->view->render('pages/about', ['commands' => $commands]));
}
}
47 changes: 47 additions & 0 deletions src/Module/Templates/DemoWeb/src/Services/CommandService.php.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.9
*/


namespace {{MODULE_NAMESPACE}}\Services;

use Quantum\Console\CommandDiscovery;
use Quantum\Service\QtService;

/**
* Class CommandService
* @package Modules\{{MODULE_NAME}}
*/
class CommandService extends QtService
{

/**
* Get all available commands (core + app)
* @return array
* @throws ReflectionException
*/
public function getAllCommands(): array
{
$coreCommands = CommandDiscovery::discover(
framework_dir() . DS . 'Console' . DS . 'Commands',
'\\Quantum\\Console\\Commands\\'
);

$appCommands = CommandDiscovery::discover(
base_dir() . DS . 'shared' . DS . 'Commands',
'\\Shared\\Commands\\'
);

return array_merge($coreCommands, $appCommands);
}
}
22 changes: 5 additions & 17 deletions src/Module/Templates/DemoWeb/src/Views/pages/about.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,18 @@
<h4><?php _t('common.version'); ?></h4>
<h6><?php _t('common.current_version', env('APP_VERSION')); ?></h6>

<h4 class="mt-4">Quantum CLI Commands</h4>
<h4 class="mt-4"><?php _t('common.cli_commands') ?></h4>

<div class="cli-list">

<?php
$commands = [
['Install debug toolbar integration', 'php qt install:debugbar'],
['Install developer toolkit', 'php qt install:toolkit'],
['Generate application key', 'php qt app:key'],
['Generate .env file', 'php qt app:env'],
['Show framework version', 'php qt app:version'],
['Show all available commands', 'php qt list'],
];
?>

<?php foreach ($commands as [$desc, $cmd]): ?>
<?php foreach ($commands as $command): ?>
<div class="cli-item">
<div class="cli-desc white-text">
<?= $desc ?>
<?php echo $command['description'] ?>
</div>

<div class="cli-command">
<code><?= $cmd ?></code>
<button class="copy-btn tooltipped" title="Copy" data-command="<?= $cmd ?>">
<code>php qt <?php echo $command['name'] ?></code>
<button class="copy-btn tooltipped" title="Copy" data-command="php qt <?php echo $command['name'] ?>">
<i class="material-icons copy-content">content_copy</i>
<i class="material-icons done-icon" style="display:none;">done</i>
</button>
Expand Down
Loading