Skip to content

Commit

Permalink
Load router type in core
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Oct 29, 2023
1 parent 3071fb3 commit ad58b1c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
4 changes: 2 additions & 2 deletions resources/Hbs/App/routerType.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class {{Class}} implements CrazyRouterType {
/**
* Parser Reg Exp
*
* Method returns regexp to parse the language code if it occurs
* Method returns regexp to parse the code if it occurs
*
* @return string regexp for parsing
*/
public static function parserRegExp():string {

# Regular expression for ISO 639-1 standard language codes
# Regular expression
return '{{Regex}}';

}
Expand Down
44 changes: 44 additions & 0 deletions src/Core/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CrazyPHP\Library\Router\Router as LibraryRouter;
use Mezon\Router\Types\BaseType as VendorBaseType;
use Mezon\Router\Router as VendorRouter;
use CrazyPHP\Interface\CrazyRouterType;
use CrazyPHP\Exception\CrazyException;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\Cache\Cache;
Expand Down Expand Up @@ -159,6 +160,9 @@ public function pushCollection(string $collectionPath = ""):void {
# Set collection
$collection = Config::get("Router");

/* Add custom type */
$this->addRouterType();

/* Add Pages */

# Check router.page
Expand Down Expand Up @@ -333,6 +337,46 @@ public function loadFromCache(string $key = ""):void {

}

/** Private methods
******************************************************
*/

/**
* Add Router Type
*
* Add router type define in the app
*
* @return void
*/
private function addRouterType():void {

# Get router type config
$routerTypeCollection = Config::getValue("Router.type");

# Check router type
if(!empty($routerTypeCollection))

# Iteration router type
foreach($routerTypeCollection as $router){

# Check name and collectio
if(
($router["name"] ?? false) &&
($router["class"] ?? false) &&
class_exists($router["class"]) &&
new $router["class"] instanceof CrazyRouterType
){

# Push in router
$this->addType(strtolower($router["name"]), $router["class"]);

}

}

}


/** Public constant
******************************************************
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ public static function getRouterPath(array $options = []):string {
# Set route name
$routeName = Context::getCurrentRoute("name");

# Get arguments
$arguments = Query::getArguments();

# Get reverse route
$result = Router::reverse((string)$routeName, Query::getArguments());
$result = Router::reverse((string)$routeName, $arguments);

# Get host name
$hostname = isset($_SERVER['REQUEST_SCHEME']) && isset($_SERVER['HTTP_HOST'])
Expand Down
5 changes: 5 additions & 0 deletions src/Library/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ public static function reverse(string $name, ?array $arguments = null):string {
# Set result
$result = $routerInstance->reverse($name, $arguments ?: []);

# Fix issue from mezon
foreach ($arguments as $name => $value) {
$result = preg_replace('/\[([A-Za-z_-]*)\:' . $name . ']/', $value, $result);
}

# Return result
return $result;

Expand Down

0 comments on commit ad58b1c

Please sign in to comment.