diff --git a/router/api.php b/router/api.php index 27191a2..9be1e80 100644 --- a/router/api.php +++ b/router/api.php @@ -1,6 +1,6 @@ api('/',function() use($router){ - $router->get('/home',function(){ - \Wepesi\Core\Response::send(['message' => 'Welcom to api routing']); +$router->api('/', function () use ($router) { + $router->get('/home', function () { + \Wepesi\Core\Http\Response::send(['message' => 'Welcom to api routing']); }); }); diff --git a/src/Core/Input.php b/src/Core/Http/Input.php similarity index 97% rename from src/Core/Input.php rename to src/Core/Http/Input.php index 140f179..1551763 100644 --- a/src/Core/Input.php +++ b/src/Core/Http/Input.php @@ -1,6 +1,6 @@ baseMiddleware = []; } - /** - * @return mixed|void - */ - protected function getMethodeUrl() - { - return $_SERVER['REQUEST_URI']; - } - /** * @param $path * @param $callable @@ -108,6 +100,7 @@ public function post(string $path, $callable, $name = null): Route { return $this->add($path, $callable, $name, 'POST'); } + /** * */ @@ -115,6 +108,7 @@ public function put(string $path, $callable, $name = null): Route { return $this->add($path, $callable, $name, 'PUT'); } + /** * */ @@ -123,13 +117,41 @@ public function delete(string $path, $callable, $name = null): Route return $this->add($path, $callable, $name, 'DELETE'); } + /** + * API base group routing + * @param string|array $base_route it can be defined as we did for group routing, but you don't need to specify api, it will be added automatically + * @param callable $callable + * @return null + */ + public function api($base_route, callable $callable) + { + $api_pattern = '/api'; + if (is_array($base_route)) { + $base_route['pattern'] = $api_pattern . (isset($base_route['pattern']) ? $this->trimPath($base_route['pattern']) : ''); + } else { + + $base_route = $api_pattern . $this->trimPath($base_route); + } + return $this->group($base_route, $callable); + } + + /** + * @param string $path + * @return string + */ + private function trimPath(string $path): string + { + $trim_path = trim($path, '/'); + return strlen($trim_path) > 0 ? '/' . $trim_path : ''; + } + /** * The group method help to group a collection of routes in to a sub-route pattern. * The sub-route pattern is prefixed into all following routes defined in the scope. * @param array|string $base_route can be a string or an array to defined middleware for the group routing * @param callable $callable a callable method can be a controller method or an anonymous callable method */ - public function group($base_route,callable $callable) + public function group($base_route, callable $callable) { $pattern = $base_route; if (is_array($base_route)) { @@ -145,29 +167,17 @@ public function group($base_route,callable $callable) } /** - * API base group routing - * @param string|array $base_route it can be defined as we did for group routing, but you don't need to specify api, it will be added automatically - * @param callable $callable - * @return null + * validate middleware data structure + * @param $middleware + * @return callable[] */ - public function api($base_route,callable $callable){ - $api_pattern = '/api'; - if (is_array($base_route)) { - $base_route['pattern'] = $api_pattern . (isset($base_route['pattern']) ? $this->trimPath($base_route['pattern']) : ''); - } else { - - $base_route = $api_pattern . $this->trimPath($base_route); + private function validateMiddleware($middleware): array + { + $valid_middleware = $middleware; + if ((is_array($middleware) && count($middleware) == 2 && is_string($middleware[0]) && is_string($middleware[1])) || is_callable($middleware)) { + $valid_middleware = [$middleware]; } - return $this->group($base_route,$callable); - } - - /** - * @param string $path - * @return string - */ - private function trimPath(string $path) :string { - $trim_path = trim($path,'/'); - return strlen($trim_path) > 0 ? '/' . $trim_path : ''; + return $valid_middleware; } /** @@ -178,7 +188,7 @@ private function trimPath(string $path) :string { public function url(string $name, array $params = []) { try { - if (! isset($this->_nameRoute[$name])) { + if (!isset($this->_nameRoute[$name])) { throw new RoutingException('No route match'); } return $this->_nameRoute[$name]->geturl($params); @@ -258,16 +268,10 @@ protected function trigger404($match = null) } /** - * validate middleware data structure - * @param $middleware - * @return callable[] + * @return mixed|void */ - private function validateMiddleware($middleware): array + protected function getMethodeUrl() { - $valid_middleware = $middleware; - if ((is_array($middleware) && count($middleware) == 2 && is_string($middleware[0]) && is_string($middleware[1])) || is_callable($middleware)) { - $valid_middleware = [$middleware]; - } - return $valid_middleware; + return $_SERVER['REQUEST_URI']; } } \ No newline at end of file diff --git a/src/Core/Validation/Validate.php b/src/Core/Validation/Validate.php index 521a01a..6a40218 100644 --- a/src/Core/Validation/Validate.php +++ b/src/Core/Validation/Validate.php @@ -7,9 +7,9 @@ namespace Wepesi\Core\Validation; use Wepesi\Core\Application; -use Wepesi\Core\Resolver\OptionsResolver; +use Wepesi\Core\Http\Response; use Wepesi\Core\Resolver\Option; -use Wepesi\Core\Response; +use Wepesi\Core\Resolver\OptionsResolver; use Wepesi\Core\Validation\Providers\Contracts\MessageBuilderContracts; /** @@ -26,6 +26,7 @@ final class Validate */ private bool $passed; private MessageErrorBuilder $message; + /** * */ @@ -65,7 +66,7 @@ function check(array $resource, array $schema) } else { foreach ($schema as $item => $rules) { if (!is_array($rules) && is_object($rules)) { - if(!$rules->generate()){ + if (!$rules->generate()) { throw new \Exception("Schema rule is not a valid schema! method generate does not exist"); } $rules = $rules->generate(); diff --git a/src/Core/View.php b/src/Core/View.php index b4a9282..fb997ce 100644 --- a/src/Core/View.php +++ b/src/Core/View.php @@ -2,15 +2,46 @@ namespace Wepesi\Core; +use DOMDocument; +use Exception; +use Wepesi\Core\Http\Response; + +/** + * + */ class View { + /** + * + */ const ERROR_VIEW = ''; + /** + * @var array + */ private static array $jslink; + /** + * @var array + */ private static array $stylelink; + /** + * @var string|null + */ private static ?string $metadata; + /** + * @var array + */ private array $data = []; + /** + * @var string + */ private string $folder_name; + /** + * @var string|null + */ private ?string $layout; + /** + * @var string|null + */ private ?string $layout_content; /** @@ -26,20 +57,12 @@ function __construct(?string $folder_name = '/') $this->layout_content = Application::$LAYOUT_CONTENT; } - /** - * @param string $folder_name - * @return void - */ - public function setFolder(string $folder_name = '/') - { - $this->folder_name = Escape::addSlaches($folder_name); - } /** * @param string $js_link * * @return void */ - public static function setJsToHead(string $js_link,bool $external = false) + public static function setJsToHead(string $js_link, bool $external = false) { self::$jslink[] = [ 'link' => $js_link, @@ -68,6 +91,15 @@ public static function setMetaData(MetaData $metadata) self::$metadata = $metadata->build(); } + /** + * @param string $folder_name + * @return void + */ + public function setFolder(string $folder_name = '/') + { + $this->folder_name = Escape::addSlaches($folder_name); + } + /** * call this method to display file content * @@ -157,10 +189,10 @@ protected function renderLayout(string $view) */ private function buildAssetHead($html) { - if(!$html){ - throwException('Unable to render empty data'); + if (!$html) { + throw new Exception('Unable to render empty data'); } - $dom = new \DOMDocument(); + $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML( mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), @@ -180,7 +212,7 @@ private function buildAssetHead($html) foreach (self::$jslink as $k => $v) { $link = $v['link']; $src = ''; - if(!$v['external']) $src = Bundles::insertJS($link,false,true); + if (!$v['external']) $src = Bundles::insertJS($link, false, true); $template->appendXML($src); $head[0]->parentNode->insertbefore($template, $head[0]->nextSibling); } @@ -213,7 +245,12 @@ public function setLayout(string $template) $this->layout = Application::$ROOT_DIR . '/views/' . $template; } - public function setLayoutContent(string $layout_name){ + /** + * @param string $layout_name + * @return void + */ + public function setLayoutContent(string $layout_name) + { $this->layout_content = $layout_name; } } \ No newline at end of file