@@ -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
0 commit comments