update: new version of bin.php

This commit is contained in:
Emmanuel ROY 2021-05-05 09:57:54 +02:00
parent aed63829d0
commit 99e7c9544c
5 changed files with 248 additions and 36 deletions

View file

@ -1,58 +1,111 @@
#!/usr/bin/php
#!/usr/bin/env php
<?php
/**
* Controlleur central du lancement des commandes
* Package MVC\Command
* @author Emmanuel ROY
* @todo protect by chosen licence between an CC-licensed or MIT-licenced (open source)
* @version 3.5
* @uses \Command SAND Console-Script of commands
*/
global $argv;
require dirname(__DIR__) . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
require dirname(__FILE__) . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
\MVC\Classe\Dumper::setPHPvalues();
//var_dump($argv);
\MVC\Command\Component\Debug::setPHPvalues();
if (isset($argv[1])) {
$option = explode(':', $argv[1]);
$command_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "command" . DIRECTORY_SEPARATOR . ucfirst($option[0]) . ".php";
//récupération des arguments de la commande pour passage en paramètre de la méthode
$arguments = array();
if (isset($argv[2])) {
for ($i = 2; $i < count($argv); $i++) {
$arguments[] = $argv[$i];
}
}
$command_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "command" . DIRECTORY_SEPARATOR . lcfirst($option[0]) . ".php";
if (is_file($command_file)) {
$class = "\MVC\Command\\" . ucfirst($option[0]);
$static_method = $option[1];
$errors = $class::$static_method(...$arguments);
$class = "Command\\" . lcfirst($option[0]);
if ($errors !== null) {
\MVC\Classe\Logger::logCommandErrors($errors);
if(isset($option[1]) && $option[1] !== '') {
$static_method = $option[1];
appel_cmd($class,$static_method);
}else{
$static_method = 'help';
$class::$static_method();
}
} else {
$command_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "command" . DIRECTORY_SEPARATOR . "App" . DIRECTORY_SEPARATOR. ucfirst($option[0]) . ".php";
$command_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "command" . DIRECTORY_SEPARATOR . "app" . DIRECTORY_SEPARATOR . lcfirst($option[0]) . ".php";
if (is_file($command_file)) {
$class = "\MVC\Command\\App\\" . ucfirst($option[0]);
$static_method = $option[1];
$errors = $class::$static_method(...$arguments);
$class = "Command\\App\\" . lcfirst($option[0]);
if ($errors !== null) {
\MVC\Classe\Logger::logCommandErrors($errors);
}
}else {
$command_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "command" . DIRECTORY_SEPARATOR . "Sand" . DIRECTORY_SEPARATOR. ucfirst($option[0]) . ".php";
if (is_file($command_file)) {
$class = "\MVC\Command\\Sand\\" . ucfirst($option[0]);
if(isset($option[1]) && $option[1] !== '') {
$static_method = $option[1];
$errors = $class::$static_method(...$arguments);
appel_cmd($class,$static_method);
if ($errors !== null) {
\MVC\Classe\Logger::logCommandErrors($errors);
}else{
$static_method = 'help';
$class::$static_method();
}
} else {
$command_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "command" . DIRECTORY_SEPARATOR . "sand" . DIRECTORY_SEPARATOR . lcfirst($option[0]) . ".php";
if (is_file($command_file)) {
$class = "Command\\Sand\\" . lcfirst($option[0]);
if(isset($option[1]) && $option[1] !== '') {
$static_method = $option[1];
appel_cmd($class,$static_method);
}else{
$static_method = 'help';
$class::$static_method();
}
}else {
print "Command not found !\n";
} else {
print "SAND Command not found !\n";
$class = "Command\\Help";
$static_method = 'help';
$class::$static_method();
}
}
}
} else {
print "No command was specified !\n";
$class = "Command\\Help";
$static_method = 'help';
$class::$static_method();
}
function appel_cmd($class,$static_method){
if(method_exists($class,$static_method)) {
//récupération des arguments de la commande pour passage en paramètre de la méthode
$arguments = array();
if (isset($argv[2])) {
for ($i = 2; $i < count($argv); $i++) {
$arguments[] = $argv[$i];
}
}
//récupération des arguments maitres "--env=????????????? ou --debug-aff=TRUEor????????"
$i = 0;
foreach ($arguments as $master_arg) {
if (preg_match("/--env=([A-Z]*)/", $master_arg, $matches)) {
define("ENV", $matches[1]);
unset($arguments[$i]);
}
if (preg_match("/--debug-aff=([A-Z]*)/", $master_arg, $matches)) {
if ($matches[1] === 'TRUE') {
define("LOG_AFF_ERROR", true);
} else {
define("LOG_AFF_ERROR", false);
}
unset($arguments[$i]);
}
$i++;
}
//appel de la commande avec les paramètres spécifiés et récupération des erreurs
$errors = $class::$static_method(...$arguments);
if ($errors !== null) {
MVC\Command\Component\Error::logErrors($errors);
}
}else{
$static_method = 'help';
$class::$static_method();
}
}

36
console/command/Help.php Normal file
View file

@ -0,0 +1,36 @@
<?php
namespace MVC\Command;
/**
* Class Cache
* Commande d'aide principale du Framework
* @package MVC\Command\Sand
*/
class Help
{
/**
* Méthode permettant d'afficher l'aide de la commande
*/
public static function help()
{
print_r(<<<EOD
Le système de commande du SAND Framework vous permet d'appeler :
\t action : permettant de créer et modifier les actions
\t cache : permettant de gérer le cache du sytème
\t conduit : permattant de créer et modifier les conduits
\t page : permattant créer, dupliquer et modifier les pages
\t symfony : permettant de gérer les modules symfony
Le système de commande de l'application vous permet d'appeler :
\t log : ou tout autre commande que vous auriez créé dans le dossier console/command/app
Vous pouvez obtenir l'aide des sous-commande en spéciant :
php bin.php macommande:help
EOD
);
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace MVC\Command\Component;
/**
* Composant permettant de debugger et d'initialiser certaines valeurs de php
* @package Default
*/
class Debug
{
/**
* Fonction Statique permettant d'initialiser les valeurs de php lors du script courant
*
* @return void
*/
public static function setPHPvalues()
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', -1);
ini_set('max_execution_time', 0);
ini_set('default_socket_timeout', -1);
date_default_timezone_set('Europe/Paris');
error_reporting(E_ALL);
return;
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace MVC\Command\Component;
/**
* Composant permettant de logger les erreurs de la commande courante
* @package Default
*/
class Error
{
/**
* Fonction courante permettant de logger les erreurs obtenues dans un fichier
*
* @param array $errors
* @return void
*/
public static function logErrors(array $errors)
{
$date = new \DateTime('now');
// log connection errors to the web service
ob_start();
foreach ($errors as $key0 => $value0) {
echo LINE_FEED.LINE_FEED."$key0 : ";
if (is_array($value0)) {
foreach ($value0 as $key1 => $value1) {
echo LINE_FEED.LINE_FEED."--$key1 : ";
if (is_array($value1)) {
foreach ($value1 as $key2 => $value2) {
echo LINE_FEED.LINE_FEED."----$key2 : ";
if (is_array($value2)) {
foreach ($value2 as $key3 => $value3) {
echo LINE_FEED.LINE_FEED."------$key3 : ";
if (is_array($value3)) {
print_r("------>".$value3. " : array");
} else {
print_r("------>".$value3);
}
}
} else {
print_r("---->".$value2);
}
}
} else {
print_r("-->".$value1);
}
}
} else {
print_r($value0);
}
}
$write_string = ob_get_clean();
file_put_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . "output" . DIRECTORY_SEPARATOR . ENV . DIRECTORY_SEPARATOR . "errors".$date->format("Y-m-d").".log", $write_string);
return;
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace MVC\Command\Component;
/**
* Composant permettant de créér des fichiers de sortie de données
* @package Default
*/
class Output
{
/**
* Fonction statique permettant d'écrire un fichier de sortie
* en lui donnant un nom particulier
*
* @param string $name
* @param string $data
* @return void
*/
public static function createOutPutFile(string $name, $data)
{
$date_debut = new \DateTime('now');
ob_start();
echo "# ".$date_debut->format('d/m/Y H:i:s') . LINE_FEED;
echo $data . LINE_FEED;
$date_fin = new \DateTime('now');
echo "# FIN ".$date_fin->format('d/m/Y H:i:s') . LINE_FEED;
$csv_file = ob_get_clean();
//on insère la date du jour au début du nom de fichier
$name = $date_debut->format("Y-m-d-").$name;
file_put_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . $name, $csv_file);
}
}