From 2fbd0c43c4d5f43c7476ceaa68ae4c0d2b704010 Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Tue, 5 Nov 2019 21:21:54 +0300 Subject: [PATCH] 191105 2121 --- src/core/Model.php | 65 +++++++++++++++++++++++++++++++++++++++++++++ src/core/Router.php | 33 ++++++++++++++++------- 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/core/Model.php b/src/core/Model.php index d93d4ce..b2cdfbf 100644 --- a/src/core/Model.php +++ b/src/core/Model.php @@ -2,6 +2,71 @@ namespace MyApp\Core; +use ReflectionClass; +use ReflectionProperty; + class Model { + /** + * @var array + */ + private $internalTableMap; + + public function __construct(?array $tableMap = null) + { + if (!is_array($tableMap)) { + $this->setInternalTableMap(); + } + } + + private function setInternalTableMap() + { + /* @var \App $app */ + global $app; + + $tableMap = ['type' => 'bd']; + + $refClass = new ReflectionClass(get_called_class()); + $tags = $app->parseTagsFromComment($refClass->getDocComment(), + 'TableName'); + if ($tags) { + if (array_key_exists('TableName', $tags)) { + $tableMap['name'] = $tags['TableName']; + } + + $properties = $refClass->getProperties(ReflectionProperty::IS_PRIVATE); + foreach ($properties as $property) { + $propertyTags = $app->parseTagsFromComment($property->getDocComment(), + 'ColumnName|ColumnOption|ColumnType'); + + if ($propertyTags) { + if (array_key_exists('ColumnName', $propertyTags)) { + $tableMap['fields'][$property->name] = $propertyTags['ColumnName']; + if (array_key_exists('ColumnOption', $propertyTags)) { + if ('id' === $propertyTags['ColumnOption']) { + $tableMap['id'] = $property->name; + } + } + if (array_key_exists('ColumnType', $propertyTags)) { + $tableMap['types'][$propertyTags['ColumnName']] = $propertyTags['ColumnType']; + } + } + } + } + $this->internalTableMap = array_key_exists('id', + $tableMap) ? $tableMap : ['type' => 'other']; + } + } + + public function find($id) + { + } + + /** + * @return array + */ + public function getInternalTableMap(): ?array + { + return $this->internalTableMap; + } } diff --git a/src/core/Router.php b/src/core/Router.php index 6b776dd..be5da76 100644 --- a/src/core/Router.php +++ b/src/core/Router.php @@ -3,6 +3,8 @@ namespace MyApp\Core; use ReflectionMethod; +use ReflectionClass; +use ReflectionException; /** * Class Router. @@ -17,8 +19,10 @@ class Router public function __construct() { $this->getRoutesFromControllers(); - $this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController', 'get404', ['GET']); - $this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController', 'post404', ['POST', 'PUT', 'DELETE']); + $this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController', + 'get404', ['GET']); + $this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController', + 'post404', ['POST', 'PUT', 'DELETE']); $this->run(); } @@ -43,7 +47,7 @@ class Router $fileName = $controllersDir.$file; if (is_file($fileName) && false !== $className) { try { - $refClass = new \ReflectionClass($controllersNamespace.$className); + $refClass = new ReflectionClass($controllersNamespace.$className); $methods = $refClass->getMethods(ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { $tags = $app->parseTagsFromComment($method->getDocComment()); @@ -53,11 +57,13 @@ class Router $tags['HttpMethod'] = ['GET']; } - $this->addRoute($tags['RouteRegExp'], $method->class, $method->name, $tags['HttpMethod']); + $this->addRoute($tags['RouteRegExp'], + $method->class, $method->name, + $tags['HttpMethod']); } } } - } catch (\ReflectionException $e) { + } catch (ReflectionException $e) { var_dump($e); } } @@ -72,10 +78,18 @@ class Router * @param string $classMethod * @param array $httpMethods */ - private function addRoute(string $regExp, string $className, string $classMethod, array $httpMethods) - { + private function addRoute( + string $regExp, + string $className, + string $classMethod, + array $httpMethods + ) { foreach ($httpMethods as $httpMethod) { - $this->routes[strtoupper($httpMethod)][] = [$regExp, $className, $classMethod]; + $this->routes[strtoupper($httpMethod)][] = [ + $regExp, + $className, + $classMethod, + ]; } } @@ -86,7 +100,8 @@ class Router { if (array_key_exists($_SERVER['REQUEST_METHOD'], $this->routes)) { foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route_array) { - if (preg_match($route_array[0], $_SERVER['REQUEST_URI'], $matches)) { + if (preg_match($route_array[0], $_SERVER['REQUEST_URI'], + $matches)) { $obj = new $route_array[1](); $param_arr = []; foreach ($matches as $key => $value) {