Skip to content

Commit 59f911c

Browse files
committed
initial commit (All the (core) files are added)
0 parents  commit 59f911c

20 files changed

+1133
-0
lines changed

Application.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace app\core;
4+
5+
use app\core\database\Database;
6+
use app\models\User;
7+
8+
class Application
9+
{
10+
/**
11+
* @var Router
12+
*/
13+
public $router;
14+
/**
15+
* @var Request
16+
*/
17+
public $request;
18+
/**
19+
* @var Response
20+
*/
21+
public $response;
22+
/**
23+
* @var View
24+
*/
25+
public $view;
26+
/**
27+
* @var Application
28+
*/
29+
public static $app;
30+
/**
31+
* @var string
32+
*/
33+
public static $ROOT_PATH;
34+
35+
/**
36+
* @var Database
37+
*/
38+
public $db;
39+
40+
/**
41+
* @var Session
42+
*/
43+
public $session;
44+
45+
/**
46+
* @var Controller
47+
*/
48+
public $controller;
49+
50+
public $userClassName;
51+
52+
/**
53+
* @param string $root_path
54+
* @param array $config
55+
*/
56+
public function __construct(string $root_path, array $config = [])
57+
{
58+
self::$ROOT_PATH = $root_path;
59+
$this->request = new Request();
60+
$this->router = new Router($this->request);
61+
$this->response = new Response();
62+
$this->view = new View($config['layout'] ?? '');
63+
self::$app = $this;
64+
$this->db = new Database($config['db'] ?? []);
65+
$this->session = new Session();
66+
$this->userClassName = $config['userClassName'] ?? '';
67+
}
68+
69+
/**
70+
* @return void
71+
*/
72+
public function run()
73+
{
74+
try
75+
{
76+
echo $this->router->resolve();
77+
}
78+
catch (\Exception $e)
79+
{
80+
$this->response->setCode($e->getCode());
81+
echo $this->view->renderView('_error', [
82+
'exception'=> $e
83+
]);
84+
}
85+
}
86+
87+
public function login(int $id): bool
88+
{
89+
$this->session->set('user', $id);
90+
return true;
91+
}
92+
93+
public static function isGuest(): bool
94+
{
95+
return !Application::$app->isLoggedIn();
96+
}
97+
98+
public static function user()
99+
{
100+
if(($id = Application::$app->isLoggedIn()) === false)
101+
{
102+
return false;
103+
}
104+
105+
return Application::$app->userClassName::find([
106+
Application::$app->userClassName::primaryKey()=> $id
107+
]);
108+
}
109+
110+
public function logout(): bool
111+
{
112+
return $this->session->remove('user');
113+
}
114+
115+
public function isLoggedIn()
116+
{
117+
return $this->session->get('user') ?? false;
118+
}
119+
}

Controller.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace app\core;
4+
5+
abstract class Controller
6+
{
7+
protected $middlewares = [];
8+
public $action;
9+
protected $model = null;
10+
11+
public function __construct(Model $model = null)
12+
{
13+
$this->model = $model;
14+
}
15+
16+
public function validate(Request $request): bool
17+
{
18+
$this->model->load($request->getBody());
19+
return $this->model->validate();
20+
}
21+
22+
public function save(): bool
23+
{
24+
return $this->model->save();
25+
}
26+
27+
/**
28+
* @param string $view
29+
* @param array $params
30+
* @return array|false|string|string[]
31+
*/
32+
public function render(string $view, array $params = [])
33+
{
34+
return Application::$app->view->renderView($view, $params);
35+
}
36+
37+
/**
38+
* @param string $layout
39+
* @return void
40+
*/
41+
public function setLayout(string $layout)
42+
{
43+
Application::$app->view->setLayout($layout);
44+
}
45+
46+
public function registerMiddleware(Middleware $middleware)
47+
{
48+
$this->middlewares[] = $middleware;
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public function getMiddlewares(): array
55+
{
56+
return $this->middlewares;
57+
}
58+
}

Middleware.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace app\core;
4+
5+
abstract class Middleware
6+
{
7+
abstract public function execute();
8+
}

Migration.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
namespace app\core;
4+
5+
use app\core\database\Database;
6+
7+
class Migration
8+
{
9+
public static $migrationTableName = 'migration';
10+
/**
11+
* @var array
12+
*/
13+
protected $migrations;
14+
15+
/**
16+
* @var Database
17+
*/
18+
public static $db;
19+
20+
/**
21+
* @var string
22+
*/
23+
public static $ROOT_PATH;
24+
25+
/**
26+
* @var string
27+
*/
28+
public static $MIGRATION_PATH;
29+
30+
/**
31+
* @var bool
32+
*/
33+
public static $saveMigrationInDb = true;
34+
35+
/**
36+
* @var int
37+
*/
38+
protected $batch = 1;
39+
protected $batchMigrated = 0;
40+
41+
/**
42+
* @param string $root_path
43+
* @param string $migration_path
44+
* @param array $config
45+
* @return void
46+
*/
47+
public function setup(string $root_path, string $migration_path, array $config = [])
48+
{
49+
static::$db = new Database($config ?? []);
50+
self::$ROOT_PATH = $root_path;
51+
self::$MIGRATION_PATH = $migration_path;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getMigrationsPath(): string
58+
{
59+
return self::$ROOT_PATH . '/' . self::$MIGRATION_PATH;
60+
}
61+
62+
/**
63+
* @return array
64+
*/
65+
public function getMigrationFiles(): array
66+
{
67+
$this->migrations = array_filter(scandir($this->getMigrationsPath()), function ($migration) {
68+
return !($migration === '.' || $migration === '..');
69+
});
70+
71+
return $this->migrations;
72+
}
73+
74+
/**
75+
* @param $message
76+
* @return void
77+
*/
78+
public function message($message)
79+
{
80+
echo $message, ' ', static::class, PHP_EOL;
81+
}
82+
83+
/**
84+
* @return void
85+
*/
86+
public function migrate()
87+
{
88+
$migrationPath = $this->getMigrationsPath();
89+
$this->batch = $this->getNextBatchNumber();
90+
91+
foreach ($this->getMigrationFiles() as $migration)
92+
{
93+
if(!$this->isMigrated($migration))
94+
{
95+
$this->batchMigrated++;
96+
$this->migrateSingleFile($migrationPath, $migration);
97+
}
98+
}
99+
100+
$this->showIfNothingIsMigrated();
101+
}
102+
103+
public function showIfNothingIsMigrated()
104+
{
105+
if(!$this->batchMigrated)
106+
{
107+
echo 'Nothing to migrate!', PHP_EOL;
108+
}
109+
}
110+
111+
public function getNextBatchNumber(): int
112+
{
113+
try {
114+
$sql = 'SELECT batch FROM ' . self::$migrationTableName . ' ORDER BY batch DESC LIMIT 1';
115+
$statement = self::$db->pdo->prepare($sql);
116+
$statement->execute();
117+
$batch = $statement->fetch(\PDO::FETCH_OBJ);
118+
119+
return ++$batch->batch;
120+
} catch (\PDOException $e)
121+
{
122+
return 1;
123+
}
124+
}
125+
126+
/**
127+
* @param string $migration
128+
* @return bool
129+
*/
130+
public function isMigrated(string $migration): bool
131+
{
132+
try {
133+
$sql = "SELECT * FROM " . self::$migrationTableName. " WHERE migration = '{$migration}' LIMIT 1";
134+
$stmt = self::$db->pdo->prepare($sql);
135+
$stmt->execute();
136+
}
137+
catch (\PDOException $e) {
138+
return false;
139+
}
140+
141+
return $stmt->rowCount() > 0;
142+
}
143+
144+
/**
145+
* @param string $migrationPath
146+
* @param string $migration
147+
* @return void
148+
*/
149+
public function migrateSingleFile(string $migrationPath, string $migration)
150+
{
151+
require $migrationPath . '/' . $migration;
152+
$class = 'app\\migrations\\' . pathinfo($migration, PATHINFO_FILENAME);
153+
$object = new $class();
154+
$object->up();
155+
156+
if($class::$saveMigrationInDb)
157+
{
158+
$this->saveMigration($migration);
159+
}
160+
}
161+
162+
public function saveMigration(string $migration)
163+
{
164+
$sql = "INSERT INTO " . self::$migrationTableName . "(migration, batch) VALUES (:migration, :batch)";
165+
$stmt = self::$db->pdo->prepare($sql);
166+
$stmt->bindValue(':migration', $migration);
167+
$stmt->bindValue(':batch', $this->batch);
168+
$stmt->execute();
169+
}
170+
}

0 commit comments

Comments
 (0)