Skip to content

Commit 6548fd0

Browse files
committed
Initial setup
1 parent 487201a commit 6548fd0

File tree

23 files changed

+479
-0
lines changed

23 files changed

+479
-0
lines changed

template/app/autoload.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* @param $className
4+
*/
5+
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+
13+
// This auto loader should go after any other vendor auto loaders so this is the end of the line
14+
require("src/" . implode('/', $parts) . '.php');
15+
}
16+
17+
spl_autoload_register('webAppFramework');

template/app/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
// Feel free to set this based on some apache configuration variable, hostname, or whatever else makese sense
3+
// for you to determine the difference between a development and production machine.
4+
// Every entry here will need a Config class and a Di class for it.
5+
define('ENVIRONMENT', 'Dev');
6+
define('PROJECT_NAMESPACE', 'YourName\YourProject');
7+
define('APP_LOCATION', '/var/www/app');
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+
/**
5+
* Created by PhpStorm.
6+
* User: Tom
7+
* Date: 2/28/2016
8+
* Time: 2:36 AM
9+
*/
10+
class ConfigBase
11+
{
12+
public static $instance;
13+
14+
/**
15+
* Protected so that this can function as a singleton
16+
*/
17+
protected function __construct()
18+
{
19+
}
20+
21+
/**
22+
* @return ConfigBase
23+
* @throws \Exception if no instance exists yet.
24+
*/
25+
public static function instance()
26+
{
27+
if (is_null(self::$instance)) {
28+
throw new \Exception("Instance does not exist to get.");
29+
}
30+
return self::$instance;
31+
}
32+
33+
/**
34+
* Called from the initialization of the project to set what di container to use for the
35+
* current environment.
36+
*/
37+
public static function setInstanceForEnvironment()
38+
{
39+
self::$instance = new static();
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace TSetliff\WebAppFramework;
4+
5+
6+
class ControllerBase
7+
{
8+
9+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace TSetliff\WebAppFramework;
3+
4+
use Exception;
5+
6+
class DiBase
7+
{
8+
private static $instance = null;
9+
private static $singletons = [];
10+
11+
/**
12+
* Protected so that this can function as a singleton
13+
*/
14+
protected function __construct()
15+
{
16+
}
17+
18+
//
19+
// This is how to get the built in classes from the base object
20+
//
21+
22+
public function getRequest()
23+
{
24+
return new Request();
25+
}
26+
27+
public function getResponse()
28+
{
29+
return new Response();
30+
}
31+
32+
/**
33+
* If you want to use twig or something you can override this and return that instead
34+
*
35+
* @return PhpTemplateWrapper
36+
*/
37+
public function getTemplateWrapper()
38+
{
39+
return new PhpTemplateWrapper();
40+
}
41+
42+
43+
//
44+
// Continue with more DI framework code
45+
//
46+
47+
/**
48+
* @return DiBase
49+
* @throws \Exception if no instance exists yet.
50+
*/
51+
public static function instance()
52+
{
53+
if (is_null(self::$instance)) {
54+
throw new \Exception("Instance does not exist to get.");
55+
}
56+
return self::$instance;
57+
}
58+
59+
/**
60+
* Called from the initialization of the project to set what di container to use for the
61+
* current environment.
62+
*/
63+
public static function setInstanceForEnvironment()
64+
{
65+
self::$instance = new static();
66+
}
67+
68+
protected function setSingleton($name, $value)
69+
{
70+
self::$singletons[$name] = $value;
71+
}
72+
73+
/**
74+
* @param $name
75+
* @param $callback
76+
* @return mixed
77+
* @throws Exception If unable to find singleton and no callback to create it.
78+
*/
79+
protected function getSingleton($name, $callback)
80+
{
81+
if (!self::hasSingleton($name)) {
82+
$singleton = $callback();
83+
self::$singletons[$name] = $singleton;
84+
return $singleton;
85+
}
86+
87+
return self::$singletons[$name];
88+
}
89+
90+
protected function hasSingleton($name)
91+
{
92+
return isset(self::$singletons[$name]);
93+
}
94+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// Set the defaults
16+
$controller = 'Default';
17+
$action = 'home';
18+
if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO']) {
19+
$parts = explode('/', $_SERVER['PATH_INFO']);
20+
21+
$requestedAction = array_pop($parts);
22+
if ($requestedAction) {
23+
$action = $requestedAction;
24+
}
25+
26+
if (count($parts)) {
27+
$requestedController = array_pop($parts);
28+
if ($requestedController) {
29+
$controller = $requestedController;
30+
}
31+
}
32+
}
33+
34+
$controllerClassName = PROJECT_NAMESPACE . '\\' . $controller . 'Controller';
35+
$controllerFileClassName = APP_LOCATION . "/src/" . PROJECT_NAMESPACE . "/Controller/{$controller}Controller.php";
36+
$actionMethodName = $action . 'Action';
37+
38+
// echo("Using $controller $action and $controllerClassName $actionMethodName file $controllerFileClassName");
39+
40+
if (file_exists($controllerFileClassName)) {
41+
$controllerClass = new $controllerClassName();
42+
if (method_exists($controllerClass, $actionMethodName)) {
43+
return $controllerClass->$actionMethodName();
44+
}
45+
}
46+
47+
// This should probably turn into a logged error and a 404 page
48+
die('Unable to process route');
49+
}
50+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace TSetliff\WebAppFramework;
3+
4+
5+
class PhpTemplateWrapper extends TemplateWrapper
6+
{
7+
public function render($templateName, $params)
8+
{
9+
require($templateName);
10+
}
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tom
5+
* Date: 2/28/2016
6+
* Time: 12:51 PM
7+
*/
8+
9+
namespace TSetliff\WebAppFramework;
10+
11+
12+
class Request
13+
{
14+
public function get($name)
15+
{
16+
if (!isset($_REQUEST[$name])) {
17+
return null;
18+
}
19+
return $_REQUEST[$name];
20+
}
21+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tom
5+
* Date: 2/28/2016
6+
* Time: 12:52 PM
7+
*/
8+
9+
namespace TSetliff\WebAppFramework;
10+
11+
12+
class Response
13+
{
14+
private $errors = [];
15+
private $messages = [];
16+
/**
17+
* @var TemplateWrapper
18+
*/
19+
private $templateWrapper;
20+
21+
/**
22+
* Response constructor.
23+
* @param TemplateWrapper|null $templateWrapper
24+
*/
25+
public function __construct(TemplateWrapper $templateWrapper = null)
26+
{
27+
if (isset($_REQUEST['errors'])) {
28+
foreach ($_REQUEST['errors'] as $errorMsg) {
29+
$this->addError($errorMsg);
30+
}
31+
}
32+
$this->templateWrapper = $templateWrapper;
33+
}
34+
35+
public function hasErrors()
36+
{
37+
return count($this->errors) > 0;
38+
}
39+
40+
public function addError($msg)
41+
{
42+
$this->errors[] = $msg;
43+
}
44+
45+
public function addHappyMessage($msg)
46+
{
47+
$this->messages[] = $msg;
48+
}
49+
50+
/**
51+
* Add the results of a rendered twig template to the output
52+
*
53+
* @param $template
54+
* @param array $params
55+
*/
56+
public function returnTemplate($template, $params = []) {
57+
$params['lastRequest'] = $_REQUEST;
58+
$params['errors'] = $this->errors;
59+
$params['messages'] = $this->messages;
60+
61+
echo((new TwigWrapper())->render($template, $params));
62+
exit(0);
63+
}
64+
65+
/**
66+
* @param string $path
67+
*/
68+
public function redirect($path = '')
69+
{
70+
$errors = '';
71+
// Allow errors to be passed through redirect for things like expiring account
72+
if (count($this->errors)) {
73+
$errors = http_build_query(['errors' => $this->errors]);
74+
}
75+
76+
$messages = '';
77+
// Allow messages to be passed through redirect for things like account successfully created
78+
if (count($this->messages)) {
79+
$messages = http_build_query(['messages' => $this->messages]);
80+
}
81+
82+
$queryIndicator = ($errors || $messages) ? '?' : '';
83+
84+
header("Location: https://{$_SERVER['HTTP_HOST']}$path$queryIndicator$errors$messages");
85+
exit(0);
86+
}
87+
88+
public function returnJson($out = [])
89+
{
90+
$out['errors'] = $this->errors;
91+
$out['messages'] = $this->messages;
92+
echo(json_encode($out));
93+
header('Content-Type: application/json');
94+
exit(0);
95+
}
96+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tom
5+
* Date: 2/28/2016
6+
* Time: 1:09 PM
7+
*/
8+
9+
namespace TSetliff\WebAppFramework;
10+
11+
12+
class TemplateWrapper
13+
{
14+
15+
}

0 commit comments

Comments
 (0)