191105 2121
Этот коммит содержится в:
родитель
499374305a
Коммит
2fbd0c43c4
@ -2,6 +2,71 @@
|
|||||||
|
|
||||||
namespace MyApp\Core;
|
namespace MyApp\Core;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
class Model
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
namespace MyApp\Core;
|
namespace MyApp\Core;
|
||||||
|
|
||||||
use ReflectionMethod;
|
use ReflectionMethod;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Router.
|
* Class Router.
|
||||||
@ -17,8 +19,10 @@ class Router
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->getRoutesFromControllers();
|
$this->getRoutesFromControllers();
|
||||||
$this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController', 'get404', ['GET']);
|
$this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController',
|
||||||
$this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController', 'post404', ['POST', 'PUT', 'DELETE']);
|
'get404', ['GET']);
|
||||||
|
$this->addRoute('/^.*$/', 'MyApp\\Controller\\ErrorsController',
|
||||||
|
'post404', ['POST', 'PUT', 'DELETE']);
|
||||||
|
|
||||||
$this->run();
|
$this->run();
|
||||||
}
|
}
|
||||||
@ -43,7 +47,7 @@ class Router
|
|||||||
$fileName = $controllersDir.$file;
|
$fileName = $controllersDir.$file;
|
||||||
if (is_file($fileName) && false !== $className) {
|
if (is_file($fileName) && false !== $className) {
|
||||||
try {
|
try {
|
||||||
$refClass = new \ReflectionClass($controllersNamespace.$className);
|
$refClass = new ReflectionClass($controllersNamespace.$className);
|
||||||
$methods = $refClass->getMethods(ReflectionMethod::IS_PUBLIC);
|
$methods = $refClass->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
$tags = $app->parseTagsFromComment($method->getDocComment());
|
$tags = $app->parseTagsFromComment($method->getDocComment());
|
||||||
@ -53,11 +57,13 @@ class Router
|
|||||||
$tags['HttpMethod'] = ['GET'];
|
$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);
|
var_dump($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,10 +78,18 @@ class Router
|
|||||||
* @param string $classMethod
|
* @param string $classMethod
|
||||||
* @param array $httpMethods
|
* @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) {
|
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)) {
|
if (array_key_exists($_SERVER['REQUEST_METHOD'], $this->routes)) {
|
||||||
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route_array) {
|
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]();
|
$obj = new $route_array[1]();
|
||||||
$param_arr = [];
|
$param_arr = [];
|
||||||
foreach ($matches as $key => $value) {
|
foreach ($matches as $key => $value) {
|
||||||
|
Загрузка…
Ссылка в новой задаче
Block a user