From a54744ffb98b1e6592f8e77c66846b1bc62a90e0 Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Sun, 10 Nov 2019 14:07:29 +0300 Subject: [PATCH] add baseInit --- src/App.php | 11 +++++++++-- src/baseInit.php | 35 +++++++++++++++++++++++++++++++++++ src/core/Model.php | 12 +++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/baseInit.php diff --git a/src/App.php b/src/App.php index a1fab61..8e58e30 100644 --- a/src/App.php +++ b/src/App.php @@ -23,7 +23,12 @@ class App */ public $db = null; - public function __construct() + /** + * App constructor. + * + * @param bool $start Start router. Default false. + */ + public function __construct(bool $start = true) { global $app; $app = $this; @@ -40,7 +45,9 @@ class App ->setControllersNamespace('\\MyApp\\Controller\\') ->setSiteName('BadPing. Nagios better'); - new Router(); + if ($start) { + new Router(); + } } /** diff --git a/src/baseInit.php b/src/baseInit.php new file mode 100644 index 0000000..8cd67d9 --- /dev/null +++ b/src/baseInit.php @@ -0,0 +1,35 @@ +config->getSrcDir() . '/' . $app->config->getClassDirs()['Model\\']; + +$files = scandir($modelsDir); +foreach ($files as $file) { + $className = strstr($file, '.php', true); + $fileName = $modelsDir . $file; + + if (is_file($fileName) && false !== $className) { + $modelName = 'MyApp\\Model\\' . $className; + /** @var Model $model */ + $model = new $modelName(); + + if ($model instanceof Model) { + if ('db' === $model->getModelType()) { + $table_map = $model->getInternalTableMap(); + + $fields = []; + foreach ($table_map['types'] as $field => $options) { + $fields[] = "`$field` $options"; + } + $fields = implode(',', $fields); + + $app->db->exec("CREATE TABLE `{$table_map['name']}` ($fields);"); + } + } + } +} diff --git a/src/core/Model.php b/src/core/Model.php index 798b0aa..7595171 100644 --- a/src/core/Model.php +++ b/src/core/Model.php @@ -22,6 +22,13 @@ class Model */ protected $app; + /** + * Model constructor. + * + * @param array|null $tableMap + * + * @throws \ReflectionException + */ public function __construct(?array $tableMap = null) { global $app; @@ -62,6 +69,9 @@ class Model } } + /** + * @throws \ReflectionException + */ private function setInternalTableMap() { /* @var \App $app */ @@ -80,7 +90,7 @@ class Model $properties = $refClass->getProperties(ReflectionProperty::IS_PRIVATE); foreach ($properties as $property) { $propertyTags = $app->parseTagsFromComment($property->getDocComment(), - 'ColumnName|ColumnOption|ColumnType|PDOType'); + 'ColumnName|ColumnOption|ColumnType'); if ($propertyTags) { if (array_key_exists('ColumnName', $propertyTags)) {