Skip to content

Commit 75d8e74

Browse files
committed
Did some tests with another project and made some changes as a result.
1 parent da8393b commit 75d8e74

File tree

12 files changed

+164
-168
lines changed

12 files changed

+164
-168
lines changed

template/app/autoload.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
* @param $className
44
*/
55
function webAppFramework($className) {
6-
$parts = explode('\\', $className);
7-
$nameSpaceRoot = array_shift($parts);
8-
if ($nameSpaceRoot == 'TSetliff') {
9-
$fileToLoad = 'framework/' . implode('/', $parts) . '.php';
10-
require($fileToLoad);
11-
}
12-
136
// This auto loader should go after any other vendor auto loaders so this is the end of the line
7+
$parts = explode('\\', $className);
148
require("src/" . implode('/', $parts) . '.php');
159
}
1610

template/app/framework/TSetliff/WebAppFramework/ControllerBase.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

template/app/framework/TSetliff/WebAppFramework/Kernel.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

template/app/framework/TSetliff/WebAppFramework/PhpTemplateWrapper.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

template/app/framework/TSetliff/WebAppFramework/Request.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

template/app/framework/TSetliff/WebAppFramework/TemplateWrapper.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

template/app/framework/TSetliff/WebAppFramework/ConfigBase.php renamed to template/app/src/TSetliff/WebAppFramework/ConfigBase.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
2-
namespace TSetliff\WebAppFramework;
3-
42
/**
5-
* Created by PhpStorm.
6-
* User: Tom
7-
* Date: 2/28/2016
8-
* Time: 2:36 AM
3+
* To keep the Config class clean, the main functionality that is extended is stored in this class.
4+
*
5+
* Do not declare any configuration here
96
*/
7+
namespace TSetliff\WebAppFramework;
8+
109
class ConfigBase
1110
{
1211
public static $instance;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace TSetliff\WebAppFramework;
3+
4+
class ControllerBase
5+
{
6+
/**
7+
* @var Request $request
8+
*/
9+
protected $request;
10+
/**
11+
* @var Response $response
12+
*/
13+
protected $response;
14+
15+
public function __construct(Request $request, Response $response)
16+
{
17+
$this->request = $request;
18+
$this->response = $response;
19+
}
20+
}

template/app/framework/TSetliff/WebAppFramework/DiBase.php renamed to template/app/src/TSetliff/WebAppFramework/DiBase.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
/**
3+
* To keep the Di class clean, the main functionality that is extended is stored in this class.
4+
*
5+
* Do not declare any dependencies here.
6+
*/
27
namespace TSetliff\WebAppFramework;
38

4-
use Exception;
5-
69
class DiBase
710
{
811
private static $instance = null;
@@ -19,27 +22,26 @@ protected function __construct()
1922
// This is how to get the built in classes from the base object
2023
//
2124

25+
/**
26+
* @return Request
27+
*/
2228
public function getRequest()
2329
{
24-
return new Request();
25-
}
26-
27-
public function getResponse()
28-
{
29-
return new Response();
30+
return $this->getSingleton('Request', function() {
31+
return new Request();
32+
});
3033
}
3134

3235
/**
33-
* If you want to use twig or something you can override this and return that instead
34-
*
35-
* @return PhpTemplateWrapper
36+
* @return Response
3637
*/
37-
public function getTemplateWrapper()
38+
public function getResponse()
3839
{
39-
return new PhpTemplateWrapper();
40+
return $this->getSingleton('Response', function() {
41+
return new Response();
42+
});
4043
}
4144

42-
4345
//
4446
// Continue with more DI framework code
4547
//
@@ -73,8 +75,8 @@ protected function setSingleton($name, $value)
7375
/**
7476
* @param $name
7577
* @param $callback
78+
*
7679
* @return mixed
77-
* @throws Exception If unable to find singleton and no callback to create it.
7880
*/
7981
protected function getSingleton($name, $callback)
8082
{
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
namespace TSetliff\WebAppFramework;
3+
4+
class Kernel {
5+
public function __construct()
6+
{
7+
// When using redirect in .htaccess instead of PATH_INFO the data is in REDIRECT_PATH_INFO
8+
if (isset($_SERVER['REDIRECT_PATH_INFO']) && $_SERVER['REDIRECT_PATH_INFO']) {
9+
$_SERVER['PATH_INFO'] = $_SERVER['REDIRECT_PATH_INFO'];
10+
}
11+
}
12+
13+
public function route()
14+
{
15+
if (!isset($_SERVER['PATH_INFO']) || !$_SERVER['PATH_INFO']) {
16+
// Just call the default controller
17+
$controllerClassName = PROJECT_NAMESPACE . '\\Controller\\DefaultController';
18+
$controllerClass = new $controllerClassName(DiBase::instance()->getRequest(), DiBase::instance()->getResponse());
19+
$controllerClass->homeAction();
20+
return;
21+
}
22+
23+
// Load Controller
24+
$parts = explode('/', $_SERVER['PATH_INFO']);
25+
// Get rid of empty stuff before first slash
26+
if (isset($parts[0]) && !$parts[0]) {
27+
array_shift($parts);
28+
}
29+
$controller = $parts[0];
30+
// Get the request object
31+
$request = DiBase::instance()->getRequest();
32+
33+
// Return error if controller does not exist
34+
$nameSpaceAsDir = str_replace('\\', '/', PROJECT_NAMESPACE);
35+
$controllerFileName = PROJECT_APP_DIR . "/src/$nameSpaceAsDir/Controller/{$controller}Controller.php";
36+
if (!file_exists($controllerFileName)) {
37+
$this->return404PageNotFound();
38+
}
39+
$controllerClassName = PROJECT_NAMESPACE . '\\Controller\\' . $controller . 'Controller';
40+
41+
/**
42+
* @var ControllerBase $controllerClass
43+
*/
44+
$controllerClass = new $controllerClassName(DiBase::instance()->getRequest(), DiBase::instance()->getResponse());
45+
46+
// Try non REST call
47+
$action = $parts[1];
48+
$actionName = $action . 'Action';
49+
if (method_exists($controllerClass, $actionName)) {
50+
return $controllerClass->$actionName();
51+
}
52+
53+
// Try REST call
54+
$restVerb = strtolower($_SERVER('REQUEST_METHOD'));
55+
$actionName = $restVerb . 'Action';
56+
if (method_exists($controllerClass, $actionName)) {
57+
$restParameters = array_splice($parts, 2);
58+
$request->setRestParameters($restParameters);
59+
return $controller->$actionName();
60+
}
61+
62+
//echo("Using $controller $action and $controllerClassName $actionName");
63+
$this->return404PageNotFound();
64+
}
65+
66+
protected function return404PageNotFound()
67+
{
68+
header("HTTP/1.0 404 Not Found");
69+
die("404 Page not found");
70+
}
71+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace TSetliff\WebAppFramework;
3+
4+
class Request {
5+
protected $restParameters = 0;
6+
7+
/**
8+
* Returns data from the request object
9+
*
10+
* @param $name
11+
* @return string
12+
*/
13+
public function get($name)
14+
{
15+
if (!isset($_REQUEST[$name])) {
16+
return null;
17+
}
18+
return $_REQUEST[$name];
19+
}
20+
21+
public function getRestParameter($index)
22+
{
23+
return $this->restParameters[$index];
24+
}
25+
26+
/**
27+
* @return int
28+
*/
29+
public function getRestParameters()
30+
{
31+
return $this->restParameters;
32+
}
33+
34+
/**
35+
* @param int $restParameters
36+
*/
37+
public function setRestParameters($restParameters)
38+
{
39+
$this->restParameters = $restParameters;
40+
}
41+
}

0 commit comments

Comments
 (0)