2019-11-04 22:14:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MyApp\Core\Config;
|
|
|
|
use MyApp\Core\Router;
|
|
|
|
|
|
|
|
class App
|
|
|
|
{
|
|
|
|
private $prefix = 'MyApp\\';
|
|
|
|
|
|
|
|
private $classDirs = [
|
|
|
|
'Controller\\' => 'controllers/',
|
|
|
|
'Core\\' => 'core/',
|
|
|
|
'Model\\' => 'models/',
|
|
|
|
'View\\' => 'views/',
|
|
|
|
];
|
|
|
|
private $basedir = __DIR__;
|
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
public $config;
|
2019-11-06 21:48:21 +00:00
|
|
|
/**
|
|
|
|
* @var \PDO
|
|
|
|
*/
|
|
|
|
public $db = null;
|
2019-11-04 22:14:53 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
global $app;
|
|
|
|
$app = $this;
|
|
|
|
spl_autoload_register([$this, 'autoload']);
|
|
|
|
|
|
|
|
$this->config = (new Config())->setDbName('db')
|
|
|
|
->setDbPort(3306)
|
|
|
|
->setDbUser('user')
|
|
|
|
->setDbPassword('password')
|
|
|
|
->setSrcDir($this->basedir)
|
|
|
|
->setTimezone('Europe/Moscow')
|
|
|
|
->setClassDirs($this->classDirs)
|
|
|
|
->setControllersDir($this->basedir.'/controllers/')
|
|
|
|
->setControllersNamespace('\\MyApp\\Controller\\')
|
|
|
|
->setSiteName('BadPing. Nagios better');
|
|
|
|
|
|
|
|
new Router();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Autoload function.
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
*/
|
|
|
|
public function autoload(string $className)
|
|
|
|
{
|
|
|
|
if (0 !== strpos($className, $this->prefix)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileName = substr($className, strlen($this->prefix));
|
|
|
|
$fileName = str_replace(array_keys($this->classDirs), array_values($this->classDirs), $fileName);
|
|
|
|
$fileName = $this->basedir.'/'.str_replace('\\', '/', $fileName).'.php';
|
|
|
|
|
|
|
|
if (file_exists($fileName)) {
|
|
|
|
require $fileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse tags from method comments.
|
|
|
|
*
|
|
|
|
* @param string $comment
|
|
|
|
* @param string $tags
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function parseTagsFromComment(string $comment, string $tags = 'RouteRegExp|HttpMethod')
|
|
|
|
{
|
|
|
|
if (preg_match_all("/^\\ *\\* \\@(?<type>$tags)\\ =\\ \"(?<value>.*)\\\".*\$/m", $comment, $matches)) {
|
|
|
|
$out = [];
|
|
|
|
foreach ($matches['type'] as $key => $type) {
|
|
|
|
if ('HttpMethod' == $type) {
|
|
|
|
$out[$type] = explode(',', $matches['value'][$key]);
|
|
|
|
} else {
|
|
|
|
$out[$type] = $matches['value'][$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|