File tree Expand file tree Collapse file tree 11 files changed +160
-1
lines changed Expand file tree Collapse file tree 11 files changed +160
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ composer.phar
2
2
/vendor /
3
3
tests /.cache
4
4
.env
5
+ cache
5
6
6
7
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
7
8
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
{
2
- "name" : " morphable/skeleton " ,
2
+ "name" : " app/app " ,
3
3
"description" : " Application skeleton" ,
4
4
"license" : " MIT" ,
5
5
"require" : {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ ];
Original file line number Diff line number Diff line change
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
+ ];
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ require __DIR__ . '/../config/bootstrap.php ' ;
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments