From 549fd7eaa2aa55bafe98c9287bcfb80fe214ce9f Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Thu, 7 Nov 2019 00:48:21 +0300 Subject: [PATCH] 191107 0048 --- docker-compose.yml | 14 +++++ public/assets/css/style.css | 6 +++ src/App.php | 4 ++ src/controllers/DefaultController.php | 1 + src/core/Model.php | 57 ++++++++++++++++++-- src/models/GroupsModel.php | 77 +++++++++++++++++++++++++++ src/models/ServersModel.php | 13 +++++ 7 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 src/models/GroupsModel.php create mode 100644 src/models/ServersModel.php diff --git a/docker-compose.yml b/docker-compose.yml index 9200437..d8b02bf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,8 +12,12 @@ services: MYSQL_USER: 'user' MYSQL_PASSWORD: 'password' MYSQL_ROOT_PASSWORD: 'rootPassword' + command: --default-authentication-plugin=mysql_native_password ports: - 3306:3306 + networks: + - backend + - frontend app: build: context: ./docker @@ -23,6 +27,8 @@ services: - "./src/:/app/src" depends_on: - mysql + networks: + - backend nginx: build: context: ./docker @@ -33,5 +39,13 @@ services: - 80:80 depends_on: - app + networks: + - frontend + - backend volumes: database: +networks: + frontend: + external: + name: proxy_proxy + backend: diff --git a/public/assets/css/style.css b/public/assets/css/style.css index e69de29..f341183 100644 --- a/public/assets/css/style.css +++ b/public/assets/css/style.css @@ -0,0 +1,6 @@ +body { + font-family: Verdana, "Geneva CY", "DejaVu Sans", sans-serif; + font-size: 1rem; + font-weight: normal; +} + diff --git a/src/App.php b/src/App.php index ee52f73..a1fab61 100644 --- a/src/App.php +++ b/src/App.php @@ -18,6 +18,10 @@ class App * @var Config */ public $config; + /** + * @var \PDO + */ + public $db = null; public function __construct() { diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index b8522bf..e955b8c 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -13,5 +13,6 @@ class DefaultController extends Controller public function index() { (new View())->index(); + $Model = new \MyApp\Model\GroupsModel(); } } diff --git a/src/core/Model.php b/src/core/Model.php index b2cdfbf..55eb0f6 100644 --- a/src/core/Model.php +++ b/src/core/Model.php @@ -2,8 +2,10 @@ namespace MyApp\Core; +use PDOException; use ReflectionClass; use ReflectionProperty; +use PDO; class Model { @@ -11,12 +13,47 @@ class Model * @var array */ private $internalTableMap; + /** + * @var \App + */ + protected $app; public function __construct(?array $tableMap = null) { - if (!is_array($tableMap)) { + global $app; + $this->app = $app; + + if ( ! is_array($tableMap)) { $this->setInternalTableMap(); } + + if ('db' === $this->getModelType()) { + $this->initDb(); + } + } + + private function initDb(): void + { + if (is_null($this->app->db)) { + try { + $this->app->db = new PDO( + 'mysql:host=mysql;dbname=' + . $this->app->config->getDbName() + . ';port=' + . $this->app->config->getDbPort(), + $this->app->config->getDbUser(), + $this->app->config->getDbPassword(), + array( + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8', + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, + PDO::ATTR_ERRMODE => true, + PDO::ATTR_PERSISTENT => true, + )); + } catch (PDOException $e) { + echo 'Подключение не удалось: ' . $e->getMessage(); + } + + } } private function setInternalTableMap() @@ -24,10 +61,10 @@ class Model /* @var \App $app */ global $app; - $tableMap = ['type' => 'bd']; + $tableMap = ['type' => 'db']; $refClass = new ReflectionClass(get_called_class()); - $tags = $app->parseTagsFromComment($refClass->getDocComment(), + $tags = $app->parseTagsFromComment($refClass->getDocComment(), 'TableName'); if ($tags) { if (array_key_exists('TableName', $tags)) { @@ -56,9 +93,16 @@ class Model $this->internalTableMap = array_key_exists('id', $tableMap) ? $tableMap : ['type' => 'other']; } + if ( ! is_array($this->internalTableMap)) { + $this->internalTableMap = ['type' => 'other']; + } } - public function find($id) + public static function find() + { + } + + public static function findOne() { } @@ -69,4 +113,9 @@ class Model { return $this->internalTableMap; } + + public function getModelType(): string + { + return $this->internalTableMap['type']; + } } diff --git a/src/models/GroupsModel.php b/src/models/GroupsModel.php new file mode 100644 index 0000000..1f29a97 --- /dev/null +++ b/src/models/GroupsModel.php @@ -0,0 +1,77 @@ +id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + * + * @return GroupsModel + */ + public function setName(string $name): self + { + $this->name = $name; + return $this; + } + + /** + * @param int $parent + * + * @return GroupsModel + */ + public function setParent(int $parent): self + { + $this->parent = $parent; + return $this; + } +} \ No newline at end of file diff --git a/src/models/ServersModel.php b/src/models/ServersModel.php new file mode 100644 index 0000000..a88b918 --- /dev/null +++ b/src/models/ServersModel.php @@ -0,0 +1,13 @@ +