Skip to content

Commit

Permalink
Added bootstrap.php
Browse files Browse the repository at this point in the history
Added tests
loading models and views are now case insensitive
  • Loading branch information
ShiniDev committed May 8, 2022
1 parent 3eddb04 commit 9d87b38
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/config/database.json
/vendor/
.env
.vscode
19 changes: 19 additions & 0 deletions bootstrap/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

define("SEP", DIRECTORY_SEPARATOR);
define("BASE_PATH", dirname(dirname(__FILE__)) . SEP);
define("RESOURCE_PATH", BASE_PATH . "resources" . SEP);
define("SOURCE_PATH", BASE_PATH . "src" . SEP);
define("CONFIG_PATH", BASE_PATH . "config" . SEP);
define("SYSTEM_PATH", SOURCE_PATH . "Sapling" . SEP);
define("APP_PATH", SOURCE_PATH . "App" . SEP);
define("CORE_PATH", SYSTEM_PATH . "Core" . SEP);
define("ENV", parse_ini_file(BASE_PATH . ".env", true));

if (ENV['ENVIRONMENT']['DEVELOPMENT'] === "TRUE") {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}

require_once BASE_PATH . "vendor" . SEP . "autoload.php";
27 changes: 2 additions & 25 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
<?php

use Sapling\Core\Kernel;
require_once dirname(dirname(__FILE__)) . "/bootstrap/bootstrap.php";

define("SEP", DIRECTORY_SEPARATOR);

define("BASE_PATH", dirname(dirname(__FILE__)) . SEP);

define("RESOURCE_PATH", BASE_PATH . "resources" . SEP);
define("SOURCE_PATH", BASE_PATH . "src" . SEP);
define("CONFIG_PATH", BASE_PATH . "config" . SEP);

define("SYSTEM_PATH", SOURCE_PATH . "Sapling" . SEP);
define("APP_PATH", SOURCE_PATH . "App" . SEP);

define("CORE_PATH", SYSTEM_PATH . "Core" . SEP);

define("ENV", parse_ini_file(BASE_PATH . ".env", true));

if (ENV['ENVIRONMENT']['DEVELOPMENT'] === "TRUE") {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}

require_once BASE_PATH . "vendor" . SEP . "autoload.php";

Kernel::run();
\Sapling\Core\Kernel::run();
4 changes: 2 additions & 2 deletions src/App/Controller/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Test extends SaplingController
public function __construct()
{
parent::__construct();
// $this->loadModel('TestModel');
$this->loadModel('testmodel');
}
public function index()
{
$this->loadView("index.php");
$this->loadView("index");
}
}
15 changes: 15 additions & 0 deletions src/Sapling/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function loadView(string $view_dir, ?array $data = null)
{
$view_dir .= '.php';
}
$view_dir = $this->getCaseInsensitiveFilename(Directories::APP_VIEW, $view_dir);
if (file_exists(Directories::APP_VIEW . $view_dir)) {
require_once Directories::APP_VIEW . $view_dir;
return true;
Expand All @@ -46,6 +47,7 @@ public function loadModel(string $model_dir, ?string $name = NULL)
{
$model_dir .= '.php';
}
$model_dir = $this->getCaseInsensitiveFilename(Directories::APP_MODEL, $model_dir);
if (file_exists(Directories::APP_MODEL . $model_dir)) {
// require_once Directories::APP_MODEL . $model_dir;
$filename = explode('/', $model_dir);
Expand Down Expand Up @@ -80,4 +82,17 @@ public function __get($name)
);
return null;
}

private function getCaseInsensitiveFilename(string $dir, string $filename)
{
$filename = strtolower($filename);
$files = scandir($dir);
for ($i = 2, $len = count($files); $i < $len; ++$i) {
if ($filename == strtolower($files[$i])) {
$filename = $files[$i];
break;
}
}
return $filename;
}
}
41 changes: 41 additions & 0 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Sapling\Controller\Controller;

final class ControllerTest extends TestCase
{
public function testLoadView(): void
{
$this->setOutputCallback(function () {
});
$controller = new Controller();
$this->assertSame(true, $controller->loadView('index'));
}

public function testFailedToLoadView(): void
{
$this->setOutputCallback(function () {
});
$controller = new Controller();
$this->assertSame(false, $controller->loadView(''));
}

public function testLoadModel(): void
{
$this->setOutputCallback(function () {
});
$controller = new Controller();
$this->assertSame(true, $controller->loadModel('TestModel'));
}

public function testFailedToLoadModel(): void
{
$this->setOutputCallback(function () {
});
$controller = new Controller();
$this->assertSame(false, $controller->loadModel(''));
}
}

0 comments on commit 9d87b38

Please sign in to comment.