Skip to content

Commit 402b424

Browse files
committed
Merge branch 'dev' of https://github.com/WebFiori/framework into dev
2 parents f14ffad + 395f667 commit 402b424

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

webfiori/framework/App.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class App {
6262
*
6363
*/
6464
const STATUS_NONE = 'NONE';
65+
/**
66+
* A constant that indicates that the status of the class is initiated.
67+
*/
68+
const STATUS_INITIATED = 'INITIATED';
6569
/**
6670
* An instance of autoloader class.
6771
*
@@ -148,6 +152,7 @@ private function __construct() {
148152
{
149153
register_shutdown_function(function()
150154
{
155+
$uriObj = Router::getRouteUri();
151156
if ($uriObj !== null) {
152157
$mdArr = $uriObj->getMiddleware();
153158

@@ -250,10 +255,8 @@ public static function getClassLoader(): ClassLoader {
250255
* the constructor of the class is not called. 'INITIALIZING' if the execution
251256
* is happening inside the constructor of the class. 'INITIALIZED' once the
252257
* code in the constructor is executed.
253-
*
254-
* @since 1.0
255258
*/
256-
public static function getClassStatus() {
259+
public static function getClassStatus() : string {
257260
return self::$ClassStatus;
258261
}
259262
/**
@@ -280,13 +283,29 @@ public static function getConfig(): ConfigurationDriver {
280283
public static function getConfigDriver() : string {
281284
return self::$ConfigDriver;
282285
}
286+
private static function getRoot() {
287+
//Following lines of code assumes that the class exist on the folder:
288+
//\vendor\webfiori\framework\webfiori\framework
289+
//Its used to construct the folder at which index file will exist at
290+
$vendorPath = '\vendor\webfiori\framework\webfiori\framework';
291+
$rootPath = substr(__DIR__, 0, strlen(__DIR__) - strlen($vendorPath));
292+
return $rootPath;
293+
}
283294
/**
284295
* Handel the request.
285296
*
286297
* This method should only be called after the application has been initialized.
287298
* Its used to handle HTTP requests or start CLI processing.
288299
*/
289300
public static function handle() {
301+
302+
if (self::$ClassStatus == self::STATUS_NONE) {
303+
$publicFolderName = 'public';
304+
self::initiate('app', $publicFolderName, self::getRoot().DIRECTORY_SEPARATOR.$publicFolderName);
305+
}
306+
if (self::$ClassStatus == self::STATUS_INITIATED) {
307+
self::start();
308+
}
290309
if (self::$ClassStatus == self::STATUS_INITIALIZED) {
291310
if (App::getRunner()->isCLI() === true) {
292311
App::getRunner()->start();
@@ -311,14 +330,17 @@ public static function handle() {
311330
* @param string $indexDir The directory at which index file exist at.
312331
* Usually, its the value of the constant __DIR__.
313332
*/
314-
public static function initiate(string $appFolder, string $publicFolder = 'public', string $indexDir = __DIR__) {
333+
public static function initiate(string $appFolder = 'app', string $publicFolder = 'public', string $indexDir = __DIR__) {
315334
if (!defined('DS')) {
316335
/**
317336
* Directory separator.
318337
*/
319338
define('DS', DIRECTORY_SEPARATOR);
320339
}
321340
if (!defined('ROOT_PATH')) {
341+
if ($indexDir == __DIR__) {
342+
$indexDir = self::getRoot().DS.$publicFolder;
343+
}
322344
/**
323345
* Path to source folder.
324346
*/
@@ -346,6 +368,7 @@ public static function initiate(string $appFolder, string $publicFolder = 'publi
346368
self::checkStandardLibs();
347369
self::checkStdInOut();
348370
self::initFrameworkVersionInfo();
371+
self::$ClassStatus = self::STATUS_INITIATED;
349372
}
350373
/**
351374
* Returns an instance which represents the class that is used to run the
@@ -432,12 +455,12 @@ public static function setConfigDriver(string $clazz) {
432455
* @since 1.0
433456
*/
434457
public static function start(): App {
435-
if (self::$ClassStatus == 'NONE') {
458+
if (self::$ClassStatus == self::STATUS_NONE || self::$ClassStatus == self::STATUS_INITIATED) {
436459
if (self::$LC === null) {
437-
self::$ClassStatus = 'INITIALIZING';
460+
self::$ClassStatus = self::STATUS_INITIALIZING;
438461
self::$LC = new App();
439462
}
440-
} else if (self::$ClassStatus == 'INITIALIZING') {
463+
} else if (self::$ClassStatus == self::STATUS_INITIALIZING) {
441464
throw new InitializationException('Using the core class while it is not fully initialized.');
442465
}
443466

webfiori/framework/middleware/MiddlewareManager.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@
1010
*/
1111
namespace webfiori\framework\middleware;
1212

13-
use webfiori\collections\LinkedList;
13+
use Exception;
14+
1415
/**
1516
* This class is used to manage the operations which are related to middleware.
1617
*
1718
* @author Ibrahim
1819
*
19-
* @since 1.0
20-
*
21-
* @since 2.0.0
2220
*/
2321
class MiddlewareManager {
2422
private static $inst;
2523
/**
2624
*
27-
* @var LinkedList
25+
* @var array
2826
*/
2927
private $middlewareList;
3028
private function __construct() {
@@ -38,8 +36,6 @@ private function __construct() {
3836
* @return array The method will return a linked list with all
3937
* middleware in the group. If no group which has the given name exist, the
4038
* list will be empty.
41-
*
42-
* @since 1.0
4339
*/
4440
public static function getGroup(string $groupName) : array {
4541
$list = [];
@@ -61,8 +57,6 @@ public static function getGroup(string $groupName) : array {
6157
* @return AbstractMiddleware|null If a middleware with the given name is
6258
* found, the method will return it. Other than that, the method will return
6359
* null.
64-
*
65-
* @since 1.0
6660
*/
6761
public static function getMiddleware(string $name) {
6862
$mdList = self::get()->middlewareList;
@@ -76,18 +70,26 @@ public static function getMiddleware(string $name) {
7670
/**
7771
* Register a new middleware.
7872
*
79-
* @param AbstractMiddleware $middleware The middleware that will be registered.
80-
*
73+
* @param AbstractMiddleware|string $middleware The middleware that will be registered.
8174
*/
82-
public static function register(AbstractMiddleware $middleware) {
83-
self::get()->middlewareList[] = $middleware;
75+
public static function register($middleware) : bool {
76+
if (gettype($middleware) == 'string') {
77+
try {
78+
$middleware = new $middleware();
79+
} catch (Exception $exc) {
80+
return false;
81+
}
82+
}
83+
if ($middleware instanceof AbstractMiddleware) {
84+
self::get()->middlewareList[] = $middleware;
85+
return true;
86+
}
87+
return false;
8488
}
8589
/**
8690
* Removes a middleware given its name.
8791
*
8892
* @param string $name The name of the middleware.
89-
*
90-
* @since 1.0
9193
*/
9294
public static function remove(string $name) {
9395
$manager = self::get();

webfiori/framework/router/RouterUri.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function getLanguages() : array {
267267
public function getMiddleware() : array {
268268
if (count($this->assignedMiddlewareList) != count($this->sortedMiddleeareList)) {
269269
$compareFunc = function ($a, $b) {
270-
return $b->getPriority() - $a->getPriority();
270+
return $a->compare($b);
271271
};
272272
$this->sortedMiddleeareList = $this->assignedMiddlewareList;
273273

0 commit comments

Comments
 (0)