diff --git a/README.md b/README.md index 4c68622..00eef4f 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,20 @@ Delete router php vendor/kzarshenas/crazyphp/bin/CrazyCommand delete router ``` +## Router Type + +New router type + +```sh +php vendor/kzarshenas/crazyphp/bin/CrazyCommand new routerType +``` + +Delete router type + +```sh +php vendor/kzarshenas/crazyphp/bin/CrazyCommand delete routerType +``` + ## Trash Clean trash @@ -90,6 +104,7 @@ php vendor/phpunit/phpunit/phpunit | CONFIG_LOCATION | @config_location | \ | Determine the location of the configs files | | ROUTER_APP_PATH | @router_app_path | \ | Determine the path of the front files of the routers | | ROUTER_CONTROLLER_PATH | @router_controller_path | \ | Determine the path of the back end controller of routers | +| ROUTER_TYPE_PATH | @router_type_path | \ | Determine the path of the back end router type | | TRASH_PATH | @trash_path | \ | Determine the path of the trash | | TRASH_DISABLE | @trash_disable | \ | Determine if the trash is disable | diff --git a/resources/Hbs/App/routerType.hbs b/resources/Hbs/App/routerType.hbs new file mode 100644 index 0000000..acceed2 --- /dev/null +++ b/resources/Hbs/App/routerType.hbs @@ -0,0 +1,59 @@ + + * @copyright 2022-2023 Kévin Zarshenas + */ +namespace {{Namespace}}; + +/** + * Dependances + */ +use CrazyPHP\Interface\CrazyRouterType; +use CrazyPHP\Core\Router; + + /** + * {{Class}} + * + * Class for manage {{Name}} router type + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ +class {{Class}} implements CrazyRouterType { + + /** + * Search Reg Exp + * + * Method returns regexp for searching this entity in the URL + * + * @return string regexp for searching + */ + public static function searchRegExp():string { + + # Return + return '(\[{{Name}}:'.Router::PARAMETER_NAME_REGEX.'\])'; + } + + /** + * Parser Reg Exp + * + * Method returns regexp to parse the language code if it occurs + * + * @return string regexp for parsing + */ + public static function parserRegExp():string { + + # Regular expression for ISO 639-1 standard language codes + return '{{Regex}}'; + + } + +} \ No newline at end of file diff --git a/resources/Yml/Router.yml b/resources/Yml/Router.yml index 57ddef8..9041b80 100644 --- a/resources/Yml/Router.yml +++ b/resources/Yml/Router.yml @@ -62,6 +62,7 @@ Router: parameters: api: format: json + type : {} methods: - get - post diff --git a/src/Cli/Core.php b/src/Cli/Core.php index 0d4118e..8e83c3c 100644 --- a/src/Cli/Core.php +++ b/src/Cli/Core.php @@ -1524,6 +1524,11 @@ private function _checkInRouter(array $inputs = []):void { "class" => "\CrazyPHP\Model\Router\Create", "parameter" => "router", ], + # Router + "routerType" => [ + "class" => "\CrazyPHP\Model\RouterType\Create", + "parameter" => "router", + ], ], ], # Command delete @@ -1539,6 +1544,11 @@ private function _checkInRouter(array $inputs = []):void { "class" => "\CrazyPHP\Model\Router\Delete", "parameter" => "routers", ], + # Router + "routerType" => [ + "class" => "\CrazyPHP\Model\RouterType\Delete", + "parameter" => "router", + ], # Trash "trash" => [ "class" => "\CrazyPHP\Model\Trash\Delete", diff --git a/src/Library/Form/Process.php b/src/Library/Form/Process.php index e835c07..a640d54 100644 --- a/src/Library/Form/Process.php +++ b/src/Library/Form/Process.php @@ -59,7 +59,8 @@ class Process { "snakeToCamel", "spaceBeforeCapital", "ucfirst", - "cleanPath" + "cleanPath", + "strtolower" ], "ARRAY" => [ ], @@ -782,6 +783,19 @@ public static function ucfirst(string $input):string { } + /** + * Lower Case + * + * @param string + * @return string + */ + public static function strtolower(string $input):string { + + # Return result + return strtolower($input); + + } + /** * Wash * diff --git a/src/Library/Router/Router.php b/src/Library/Router/Router.php index c3f04aa..196d663 100644 --- a/src/Library/Router/Router.php +++ b/src/Library/Router/Router.php @@ -583,6 +583,7 @@ public static function reverse(string $name, ?array $arguments = null):string { * Get Summary * * Get a summary of existing routers + * * @return array */ public static function getSummary():array { @@ -620,6 +621,43 @@ public static function getSummary():array { } + /** + * Get Router Type Summary + * + * Get a summary of existing router type + * + * @return array + */ + public static function getRouterTypeSummary():array { + + # Set result + $result = []; + + # Get routers + $routerTypeCollection = Config::getValue("Router.type"); + + # Check routers + if(is_array($routerTypeCollection) && !empty($routerTypeCollection)){ + + # Iteration routers + foreach($routerTypeCollection as $routerType){ + + # Check router name + if(isset($routerType["name"]) && $routerType["name"]){ + + $result[$routerType["name"]] = ucfirst($routerType["name"]); + + } + + } + + } + + # Return result + return $result; + + } + /** Public static methods | Routers file ****************************************************** */ @@ -660,6 +698,24 @@ public static function getControllerPath():string { } + /** + * Get Router Type Path + * + * Get path of the custom router type + * env : "router_type_path" + * + * @return string + */ + public static function getRouterTypePath():string { + + # Set result + $result = Env::get("router_type_path", true) ?: static::ROUTER_TYPE_PATH; + + # Return result + return $result; + + } + /** Public constants ****************************************************** */ @@ -683,4 +739,7 @@ public static function getControllerPath():string { /** @const public ROUTER_CONTROLLER_PATH */ public const ROUTER_CONTROLLER_PATH = "@app_root/app/Controller/"; + /** @const public ROUTER_TYPE_PATH */ + public const ROUTER_TYPE_PATH = "@app_root/app/Library/Router/Type/"; + } \ No newline at end of file diff --git a/src/Model/RouterType/Create.php b/src/Model/RouterType/Create.php new file mode 100644 index 0000000..356643b --- /dev/null +++ b/src/Model/RouterType/Create.php @@ -0,0 +1,240 @@ + + * @copyright 2022-2023 Kévin Zarshenas + */ +namespace CrazyPHP\Model\RouterType; + +/** + * Dependances + */ +use CrazyPHP\Library\File\Config as FileConfig; +use CrazyPHP\Library\Template\Handlebars; +use CrazyPHP\Library\Model\CrazyModel; +use CrazyPHP\Exception\CrazyException; +use CrazyPHP\Interface\CrazyCommand; +use CrazyPHP\Library\Router\Router; +use CrazyPHP\Library\Array\Arrays; +use CrazyPHP\Library\Form\Process; +use CrazyPHP\Library\File\File; +use CrazyPHP\Model\Env; + +/** + * Create new Router + * + * Classe for create step by step new router + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ +class Create extends CrazyModel implements CrazyCommand { + + /** Public constants + ****************************************************** + */ + + public const REQUIRED_VALUES = [ + # Name + [ + "name" => "name", + "description" => "Name of your crazy router type", + "default" => "test", + "type" => "VARCHAR", + "required" => true, + "process" => ['cleanPath', 'snakeToCamel', 'strtolower', 'trim'] + ], + # Methods + [ + "name" => "regex", + "description" => "Regex to use for catch token", + "default" => "(.*)", + "type" => "VARCHAR", + ] + ]; + + /** Private Parameters + ****************************************************** + */ + + /** + * Inputs + */ + private $inputs = []; + + /** @var array $router */ + private $routerType = []; + + /** + * Constructor + * + * Ingest data + * + * @param array $formResult Collection of value to process + * @return Create + */ + public function __construct(array $inputs = []){ + + # Ingest data + $this->inputs = $inputs; + + } + + /** Public static methods + ****************************************************** + */ + + /** + * Get Required Values + * + * Return required values + * + * @return array + */ + public static function getRequiredValues():array { + + # Set result + $result = self::REQUIRED_VALUES; + + # Return result + return $result; + + } + + /** Public methods + ****************************************************** + */ + + /** + * Run creation of project + * + * @return Create + */ + public function run():self { + + /** + * Run Prepare Router + * - Process input to router object + */ + $this->runPrepareRouterType(); + + /** + * Run Create Script File + * - Create script file + */ + $this->runCreateScriptFile(); + + /** + * Run Router In Config + * - Integrate Router into config + */ + $this->runRouterIntoConfig(); + + # Return this + return $this; + + } + + /** Public methods | Run + ****************************************************** + */ + + /** + * Run Prepare Router Type + * + * Process input to router type object + * + * @return void + */ + public function runPrepareRouterType():void { + + # Process inputs + $this->routerType = Process::getResultSummary($this->inputs["router"]); + + # Get Router collection + $routerTypeCollection = FileConfig::getValue("Router.type"); + + # Clean router name + $routerTypeName = Process::snakeToCamel(str_replace(["/", "."], "_", $this->routerType["Name"]), true); + + # Check if router name alreay exists + if(!empty(@Arrays::filterByKey($routerTypeCollection, "name", $this->routerType["name"]))) + + # New error + throw new CrazyException( + "Given name \"$routerTypeName\" already exists in router type collection", + 500, + [ + "custom_code" => "create-router-type-001", + ] + ); + + # Set up env for cache driver + Env::set([ + "cache_driver" => "Files" + ]); + + } + + /** + * Run Create Controler File + * + * Create the php controller file + * + * @return void + */ + public function runCreateScriptFile():void { + + # Set additionnal data + $this->routerType["Namespace"] = "App\\Library\\Router\\Type"; + $this->routerType["Class"] = ucfirst($this->routerType["Name"]); + $this->routerType["Controller"] = $this->routerType["Namespace"]."\\".$this->routerType["Class"]; + + # Create template instance + $template = new Handlebars(); + + # Load template + $template->load("@crazyphp_root/resources/Hbs/App/routerType.hbs"); + + # Render template with current router value + $result = $template->render($this->routerType); + + # File path + $filePath = Router::getRouterTypePath()."/".$this->routerType["Class"].".php"; + + # Create file + File::create($filePath, $result); + + } + + /** + * Run Router In Config + * + * Integrate Router into config + * + * @return void + */ + public function runRouterIntoConfig():void { + + # Get router collection count + $routerTypeCollection = FileConfig::getValue("Router.type"); + + # Count routers + $routerTypeKey = count($routerTypeCollection ?: []); + + # Set value in config + FileConfig::setValue("Router.type.".$routerTypeKey, [ + "name" => $this->routerType["Name"], + "class" => $this->routerType["Controller"] + ]); + + } + +} \ No newline at end of file diff --git a/src/Model/RouterType/Delete.php b/src/Model/RouterType/Delete.php new file mode 100644 index 0000000..0efe9fa --- /dev/null +++ b/src/Model/RouterType/Delete.php @@ -0,0 +1,280 @@ + + * @copyright 2022-2023 Kévin Zarshenas + */ +namespace CrazyPHP\Model\RouterType; + +/** + * Dependances + */ +use CrazyPHP\Library\Model\CrazyModel; +use CrazyPHP\Exception\CrazyException; +use CrazyPHP\Interface\CrazyCommand; +use CrazyPHP\Library\Router\Router; +use CrazyPHP\Library\Array\Arrays; +use CrazyPHP\Library\File\Config; +use CrazyPHP\Model\Router\Create; +use CrazyPHP\Library\File\Trash; +use CrazyPHP\Library\File\File; + +/** + * Delete Router + * + * Classe for deletion of router + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ +class Delete extends CrazyModel implements CrazyCommand { + + /** Public constants + ****************************************************** + */ + + public const REQUIRED_VALUES = [ + # Type + [ + "name" => "routers", + "description" => "Routers to delete", + "type" => "ARRAY", + "required" => true, + "multiple" => true, + "select" => "CrazyPHP\Library\Router\Router::getRouterTypeSummary" + ], + ]; + + /** Parameters + ****************************************************** + */ + + /** @var array $inputs */ + private array $inputs = []; + + /** @var array $routerType */ + private array $routerType = []; + + /** + * Constructor + * + * Construct current class + * + * @return Create + */ + public function __construct(array $inputs = []){ + + # Set inputs + $this->inputs = $inputs; + + } + + /** Public static methods + ****************************************************** + */ + + /** + * Get Required Values + * + * Return required values + * + * @return array + */ + public static function getRequiredValues():array { + + # Declare result + $result = self::REQUIRED_VALUES; + + # Return result + return $result; + + } + + /** Public method + ****************************************************** + */ + + /** + * Run delete of project + * + * @return Delete + */ + public function run():self { + + /** + * Run Retrieve Router + * - Process input to retrieve object + */ + $this->runRetrieveRouter(); + + /** + * Run Remove From Config + * - Remove Router from config + */ + $this->runRemoveFromConfig(); + + /** + * Run Delete Controler File + * - Delete the php controller file + */ + $this->runDeleteScriptFile(); + + /** + * Run Remove Folder + * - Remove folder in environnement + */ + $this->runRemoveFolder(); + + # Return this + return $this; + + } + + /** Public methods | Run + ****************************************************** + */ + + /** + * Run Retrieve Router + * + * Process input to retrieve object + * + * @return void + */ + public function runRetrieveRouter():void { + + # Get routers to delete + $routerTypeCollection = $this->inputs["router"][0]["value"]; + + # Check routers + if(empty($routerTypeCollection)) + + # New error + throw new CrazyException( + "None router selected.", + 500, + [ + "custom_code" => "create-router-001", + ] + ); + + # Iteration of routers + foreach($routerTypeCollection as $router){ + + # Push in inputs + $this->routerType[] = [ + "name" => $router, + ]; + + } + + } + + /** + * Run Remove From Config + * + * Remove Router from config + * + * @return void + */ + public function runRemoveFromConfig():void { + + # Iteration routers + foreach($this->routerType as $routerType){ + + # Open config of the current type + $routerTypeCollection = Config::getValue("Router.type"); + + # Check routers + if(is_array($routerTypeCollection)){ + + # Search router in collection + $search = Arrays::filterByKey($routerTypeCollection, "name", $routerType["name"]); + + # Check search + if(!empty($search)){ + + # Set Router key + $setRouterKey = array_key_first($search); + + # Send to trash + Trash::sendAnObject( + [ + "Router" => [ + "type" => $search + ] + ], + "config", + "router/type/".$routerType["name"] + ); + + # Remove this config from router config + Config::removeValue("Router.type.$setRouterKey"); + + } + + } + + } + + } + + /** + * Run Delete Script File + * + * Delete the php script file + * + * @return void + */ + public function runDeleteScriptFile():void { + + # Iteration routers + foreach($this->routerType as $routerType){ + + # Set index path + $controllerPath = Router::getRouterTypePath()."/".ucfirst($routerType["name"]).".php"; + + # Check index exits + if(!File::exists($controllerPath)) + + # Continue iteration + continue; + + # Send file to trash + Trash::send( + $controllerPath, + "routerType/".$routerType["name"] + ); + + } + + } + + /** + * Run Remove Folder + * + * Remove folder in environnement + * + * @return void + */ + public function runRemoveFolder():void { + + # Get router type path + $folder = Router::getRouterTypePath(); + + # Check if empty + if(File::isEmpty($folder)) + + # Remove folder + File::remove($folder); + + } + +} \ No newline at end of file