Skip to content

Commit 011a571

Browse files
committed
skeleton example
1 parent bda3ad3 commit 011a571

File tree

11 files changed

+160
-1
lines changed

11 files changed

+160
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ composer.phar
22
/vendor/
33
tests/.cache
44
.env
5+
cache
56

67
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
78
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file

App/Domain/.gitkeep

Whitespace-only changes.

App/Infrastructure/Application.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Infrastructure;
4+
5+
use \Morphable\SimpleContainer;
6+
7+
class Application
8+
{
9+
private static $paths;
10+
11+
private static $services;
12+
13+
public static function initialize(string $root)
14+
{
15+
self::$paths = [];
16+
self::$paths['root'] = $root;
17+
self::$paths['config'] = $root . '/config';
18+
self::$paths['app'] = $root . '/App';
19+
self::$paths['public'] = $root . '/public';
20+
self::$paths['views'] = $root . '/views';
21+
self::$paths['cache'] = $root . '/cache';
22+
}
23+
24+
public static function getPath(string $name = null)
25+
{
26+
if ($name != null) {
27+
return self::$paths[$name];
28+
}
29+
30+
return self::$paths;
31+
}
32+
33+
public static function addService($name, $obj)
34+
{
35+
if (self::$services == null) {
36+
self::$services = new SimpleContainer();
37+
}
38+
39+
self::$services->add($name, $obj);
40+
}
41+
42+
public static function getService($name)
43+
{
44+
if (self::$services == null) {
45+
self::$services = new SimpleContainer();
46+
}
47+
48+
return self::$services->get($name);
49+
}
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Infrastructure\Controller;
4+
5+
use App\Infrastructure\Application;
6+
7+
class StaticPageController
8+
{
9+
public static function serveHome($req, $res)
10+
{
11+
return $res->sendResponse(
12+
Application::getService("view")->serve("home.php", [
13+
'title' => 'hello world'
14+
])
15+
);
16+
}
17+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "morphable/skeleton",
2+
"name": "app/app",
33
"description": "Application skeleton",
44
"license": "MIT",
55
"require": {

config/bootstrap.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use \Morphable\SimpleRouting;
4+
use \Morphable\SimpleRouting\Builder;
5+
use \App\Infrastructure\Application;
6+
7+
// require vendor
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
// intialize application
11+
Application::initialize(__DIR__ . '/..');
12+
13+
// get predefined services and add them to application
14+
$services = require Application::getPath('config') . '/services.php';
15+
16+
foreach ($services as $name => $service) {
17+
Application::addService($name, $service);
18+
}
19+
20+
// get predefined routes and add them to the router
21+
$routes = require Application::getPath('config') . '/routes.php';
22+
23+
foreach($routes as $name => $route) {
24+
SimpleRouting::add($name, Builder::fromArray($route));
25+
}
26+
27+
// execute router and catch errors
28+
try {
29+
SimpleRouting::execute();
30+
} catch (\Morphable\SimpleRouting\Exception\RouteNotFound $e) {
31+
echo Application::getService('view')->serve('errors/404.php');
32+
die;
33+
}

config/routes.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use \App\Infrastructure\Controller\StaticPageController;
4+
5+
// predefine routes with static method as callback
6+
return [
7+
'index' => [
8+
'method' => 'GET',
9+
'route' => '/',
10+
'callback' => [StaticPageController::class, 'serveHome']
11+
],
12+
'home' => [
13+
'method' => 'GET',
14+
'route' => '/home',
15+
'callback' => [StaticPageController::class, 'serveHome']
16+
]
17+
];

config/services.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use \App\Infrastructure\Application;
4+
use \Morphable\SimpleCache;
5+
use \Morphable\SimpleView;
6+
use \Morphable\SimpleDebugger;
7+
use \Morphable\SimpleDatabase;
8+
9+
// define services, name => instance
10+
return [
11+
'cache' => new SimpleCache(Application::getPath('cache')),
12+
'view' => new SimpleView(Application::getPath('views')),
13+
'debug' => new SimpleDebugger()
14+
];

public/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require __DIR__ . '/../config/bootstrap.php';

views/errors/404.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<h1>404</h1>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)