Skip to content

Commit

Permalink
Merge pull request #49 from studoo-app/48-implement-binconsole
Browse files Browse the repository at this point in the history
#48 implement binconsole
  • Loading branch information
bfoujols authored Mar 12, 2024
2 parents 44b18db + 64aca71 commit 2fea097
Show file tree
Hide file tree
Showing 21 changed files with 438 additions and 19 deletions.
28 changes: 28 additions & 0 deletions bin/edu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env php
<?php

// Envoie les erreurs à stderr, pas à stdout. ou (0)
ini_set('display_errors', 'stderr');

// Évitez d'afficher deux fois les erreurs PHP.
ini_set('log_errors', '0');

// Masquer les deprecations de PHP 8.1
error_reporting(E_ALL & ~E_DEPRECATED);

if (version_compare(PHP_VERSION, '8.1', '<')) {
printf("Cet outil nécessite au moins PHP8.1. %s est actuellement installé. Veuillez mettre à jour votre version de PHP.\n", PHP_VERSION);
exit(1);
}

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
} else {
fwrite(STDERR, 'ERROR: Les dépendances du gestionnaire de package (composer) ne sont pas correctement configurées! Exécutez "composer install" ou consultez README.md pour plus de détails' . PHP_EOL);
exit(1);
}

define('ROOT_PATH', realpath('.'));

$application = new \Studoo\EduFramework\Commands\Extends\AppCommand();
$application->run();
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.1",
"vlucas/phpdotenv": "^v5.5",
"twig/twig": "^3.7",
"nikic/fast-route": "^1.3",
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ parameters:
excludePaths:
analyse:
- src/EduFramework/Scripts
- src/EduFramework/Commands
- src/EduFramework/Commands
- bin
- app
7 changes: 3 additions & 4 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Masquer les deprecations de PHP 8.1
error_reporting(E_ALL & ~E_DEPRECATED);

if (version_compare(PHP_VERSION, '8.0', '<') === false) {
if (version_compare(PHP_VERSION, '8.1', '<') === false) {
// Autoloader => chargement automatique des classes depuis le dossier vendor/
require_once __DIR__ . '/../vendor/autoload.php';

Expand All @@ -25,7 +25,6 @@
// Chargement des classes utilisées par l'application
(new LoadCouchCore())->run();
} else {
echo "Cet app nécessite au moins PHP8.0. "
. PHP_VERSION .
" est actuellement installé. Veuillez mettre à jour votre version de PHP.\n";
printf("Cet app nécessite au moins PHP8.1.");
printf(" Veuillez mettre à jour votre version de PHP.\n");
}
15 changes: 9 additions & 6 deletions src/EduFramework/Commands/CreateControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Studoo\EduFramework\Commands\Exception\RouteAlreadyExistsException;
use Studoo\EduFramework\Commands\Exception\ViewAlreadyExistsException;
use Studoo\EduFramework\Core\Controller\ControllerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -16,21 +17,23 @@

/**
* Class CreateControllerCommand
* Classe permettant l'utilisation de la commande edu:make:controller
* Classe permettant l'utilisation de la commande php bin/edu make:controller <controller-name>
* Cette commande permet de générer un controller, sa vue associée
* ainsi que déclarer la route dans le fichier de configuration
* @package Studoo\EduFramework\Commands
*/
#[AsCommand(
name: 'make:controller',
description: 'Génération d un controller',
)]
class CreateControllerCommand extends Command
{
private const ROUTES_FILE_PATH = './app/Config/routes.yaml';
private const TEMPLATES_DIR = './app/Template/';

protected function configure(): void
{
$this->setDefinition([
new InputArgument('controller-name', InputArgument::REQUIRED, 'Controller name'),
]);
$this->AddArgument('controller-name', InputArgument::REQUIRED, 'Controller name');
}

/**
Expand All @@ -46,7 +49,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);

//Format controller-name arg
$namesCollection = self::getNamesCollection($input->getFirstArgument());
$namesCollection = self::getNamesCollection($input->getArgument('controller-name'));
//Generate route params in app/Config/routes.yaml
self::generateRoute($namesCollection["uri"], $namesCollection["uri"], $namesCollection["className"]);
//Generate Controller Class
Expand All @@ -71,7 +74,7 @@ private function getNamesCollection(string $arg): array
array_shift($pieces);

$className = ucfirst($arg)."Controller";
$uri = "/".strtolower($arg);
$uri = strtolower($arg);
$twig = strtolower($arg);

if(count($pieces) > 1) {
Expand Down
54 changes: 54 additions & 0 deletions src/EduFramework/Commands/DefaultCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Studoo\EduFramework\Commands;

use Studoo\EduFramework\Commands\Extends\CkeckStack;
use Studoo\EduFramework\Commands\Extends\CommandBanner;
use Studoo\EduFramework\Commands\Extends\CommandManage;
use Studoo\EduFramework\Commands\Extends\listCommand;
use Studoo\EduFramework\Core\ConfigCore;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class DefaultCommand
* Classe permettant l'utilisation de la commande par défaut
* @package Studoo\EduFramework\Commands
* Example command line:
* ```
* $ php bin/edu default
* ```
*/
#[AsCommand(
name: 'default',
description: 'Liste des commandes disponibles',
)]
class DefaultCommand extends CommandManage
{
/**
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
self::$stdOutput->writeln([
CommandBanner::getBanner(),
'Bienvenue dans la console ' . ConfigCore::getConfig('name'),
''
]);

$check = new CkeckStack($output, self::$stdOutput);
$check->render();

$check = new listCommand($output, self::$stdOutput);
$check->render();

self::$stdOutput->writeln([
'Si vous avez un problème, https://github.com/studoo-app/edu-framework/discussions',
''
]);

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class ControllerAlreadyExistsException extends \Exception
{

/**
* Message de l'exception
* @var string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class RouteAlreadyExistsException extends \Exception
{

/**
* Message de l'exception
* @var string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class ViewAlreadyExistsException extends \Exception
{

/**
* Message de l'exception
* @var string
Expand Down
20 changes: 20 additions & 0 deletions src/EduFramework/Commands/Extends/AppCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Studoo\EduFramework\Commands\Extends;

use Studoo\EduFramework\Core\ConfigCore;
use Symfony\Component\Console\Application;

class AppCommand extends Application
{
public function __construct()
{
(new ConfigCore([]));
parent::__construct(ConfigCore::getConfig('name'), ConfigCore::getConfig('version'));

$this->add(new \Studoo\EduFramework\Commands\DefaultCommand());
$this->add(new \Studoo\EduFramework\Commands\CreateControllerCommand());

$this->setDefaultCommand('default');
}
}
68 changes: 68 additions & 0 deletions src/EduFramework/Commands/Extends/CkeckStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Studoo\EduFramework\Commands\Extends;

use Studoo\EduFramework\Core\ConfigCore;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Class CheckStack
* Gestion des prerequis
*
* @author Benoit Foujols
*/
class CkeckStack
{
/**
* @var OutputInterface
*/
private OutputInterface $output;

/**
* @var SymfonyStyle
*/
private SymfonyStyle $symfonyStyle;

public function __construct(OutputInterface $output, SymfonyStyle $symfonyStyle)
{
$this->output = $output;
$this->symfonyStyle = $symfonyStyle;
}

/**
* Rendu des prérequis dans le terminal
*
* @return void
*/
public function render(): void
{
$this->symfonyStyle->writeln([
'Check votre env. : ',
]);
$table = new Table($this->output);
$table
->setHeaders(['CHECK', 'SERVICE', 'VERSION'])
->setRows($this->run());
$table->render();
$this->symfonyStyle->writeln([
'',
]);
}

/**
* Execution des tests de prerequis
*
* @return array
*/
private function run(): array
{
$listCheck = [];

$listCheck[] = (version_compare(PHP_VERSION, ConfigCore::getConfig('php_version'), '>=') === true) ? ["OK", 'PHP', PHP_VERSION] : ["KO", 'PHP', PHP_VERSION];
$listCheck[] = ["INFO", 'PHP', PHP_BINARY];

return $listCheck;
}
}
90 changes: 90 additions & 0 deletions src/EduFramework/Commands/Extends/CommandBanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Studoo\EduFramework\Commands\Extends;

use DateTime;
use DateTimeZone;
use Studoo\EduFramework\Core\ConfigCore;

/**
* Class CommandBanner
* Gestion de la banniere de loader
*
* @author Benoit Foujols
*/
class CommandBanner
{
/**
* @var DateTime
*/
private static DateTime $timeExecStart;

/**
* @var float|string
*/
private static float|string $timeExecStartMicro;

/**
* Banner of the command
*
* @return string
* @throws \Exception
* @var $message string Add text in banner
*/
public static function getBanner(): ?string
{
$date = new \DateTime("now", new DateTimeZone("Europe/Paris"));
self::$timeExecStart = $date;
self::$timeExecStartMicro = microtime(true);

$banner = "<info>";
$banner .= " _ __ \n";
$banner .= " ___ __| |_ _ / _|_ __ __ _ _ __ ___ ___ \n";
$banner .= " / _ \/ _` | | | | | |_| '__/ _` | '_ ` _ \ / _ \ \n";
$banner .= " | __/ (_| | |_| | | _| | | (_| | | | | | | __/ \n";
$banner .= " \___|\__,_|\__,_| |_| |_| \__,_|_| |_| |_|\___| \n";
$banner .= " </info><comment>" . ConfigCore::getConfig('version') . " ";
$banner .= ConfigCore::getConfig('date_version') . " by studoo collectif</comment> \n";

return $banner;
}

/**
* Footer of the command
*
* @return String|null
* @throws \Exception
*/
public static function getEnd(): ?string
{
$banner = "\n<info>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</info>\n";
$banner .= "<comment>Command launched : </comment> \n";
$banner .= "<comment>Version : " . ConfigCore::getConfig('version') . "</comment> \n";
$banner .= "<comment>Running time : </comment>" . self::execTime() . "\n";
$banner .= "<info>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</info>\n";

return $banner;
}

/**
* Calculate Exec Time Command
*
* @return String|null
* @throws \Exception
*/
private static function execTime(): ?string
{
// Calcul Seconde
$dateEnd = new \DateTime("now", new DateTimeZone('Europe/Paris'));
$dateDiff = self::$timeExecStart->diff($dateEnd);
// Calcul MS
$diffMicro = microtime(true) - self::$timeExecStartMicro;

if ($diffMicro > 1) {
$microSec = explode(".", $diffMicro);
return $dateDiff->format("%H:%I:%S") . "(" . substr($microSec[1], 0, 3) . "ms)";
}

return round($diffMicro, 2) . " ms.";
}
}
Loading

0 comments on commit 2fea097

Please sign in to comment.