add baseInit
Этот коммит содержится в:
родитель
3e3cfb4cf0
Коммит
a54744ffb9
@ -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,8 +45,10 @@ class App
|
||||
->setControllersNamespace('\\MyApp\\Controller\\')
|
||||
->setSiteName('BadPing. Nagios better');
|
||||
|
||||
if ($start) {
|
||||
new Router();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Autoload function.
|
||||
|
35
src/baseInit.php
Обычный файл
35
src/baseInit.php
Обычный файл
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use MyApp\Core\Model;
|
||||
|
||||
include 'App.php';
|
||||
|
||||
$app = new App(false);
|
||||
|
||||
$modelsDir = $app->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);");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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)) {
|
||||
|
Загрузка…
Ссылка в новой задаче
Block a user