Skip to content

Commit 3f6e4bb

Browse files
committed
Initial commit
1 parent 941f17b commit 3f6e4bb

File tree

643 files changed

+37987
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

643 files changed

+37987
-0
lines changed

App/Config.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App;
4+
5+
/**
6+
* Application configuration
7+
*
8+
* PHP version 5.4
9+
*/
10+
class Config
11+
{
12+
13+
/**
14+
* Database host
15+
* @var string
16+
*/
17+
const DB_HOST = 'your-database-host';
18+
19+
/**
20+
* Database name
21+
* @var string
22+
*/
23+
const DB_NAME = 'your-database-name';
24+
25+
/**
26+
* Database user
27+
* @var string
28+
*/
29+
const DB_USER = 'your-database-user';
30+
31+
/**
32+
* Database password
33+
* @var string
34+
*/
35+
const DB_PASSWORD = 'your-database-password';
36+
37+
/**
38+
* Show or hide error messages on screen
39+
* @var boolean
40+
*/
41+
const SHOW_ERRORS = true;
42+
}

App/Controllers/Home.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use \Core\View;
6+
7+
/**
8+
* Home controller
9+
*
10+
* PHP version 5.4
11+
*/
12+
class Home extends \Core\Controller
13+
{
14+
15+
/**
16+
* Show the index page
17+
*
18+
* @return void
19+
*/
20+
public function indexAction()
21+
{
22+
View::renderTemplate('Home/index.html');
23+
}
24+
}

App/Models/User.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use PDO;
6+
7+
/**
8+
* Example user model
9+
*
10+
* PHP version 5.4
11+
*/
12+
class User extends \Core\Model
13+
{
14+
15+
/**
16+
* Get all the users as an associative array
17+
*
18+
* @return array
19+
*/
20+
public static function getAll()
21+
{
22+
$db = static::getDB();
23+
$stmt = $db->query('SELECT id, name FROM users');
24+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
25+
}
26+
}

App/Views/404.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
3+
{% block title %}Page not found{% endblock %}
4+
5+
{% block body %}
6+
7+
<h1>Page not found</h1>
8+
<p>Sorry, that page doesn't exist.</p>
9+
10+
{% endblock %}

App/Views/500.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
3+
{% block title %}Error{% endblock %}
4+
5+
{% block body %}
6+
7+
<h1>An error occurred</h1>
8+
<p>Sorry, an error has occurred.</p>
9+
10+
{% endblock %}

App/Views/Home/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "base.html" %}
2+
3+
{% block title %}Home{% endblock %}
4+
5+
{% block body %}
6+
7+
<h1>Welcome</h1>
8+
9+
{% endblock %}

App/Views/base.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>{% block title %}{% endblock %}</title>
6+
</head>
7+
<body>
8+
{% block body %}
9+
{% endblock %}
10+
</body>
11+
</html>

Core/Controller.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
/**
6+
* Base controller
7+
*
8+
* PHP version 5.4
9+
*/
10+
abstract class Controller
11+
{
12+
13+
/**
14+
* Parameters from the matched route
15+
* @var array
16+
*/
17+
protected $route_params = [];
18+
19+
/**
20+
* Class constructor
21+
*
22+
* @param array $route_params Parameters from the route
23+
*
24+
* @return void
25+
*/
26+
public function __construct($route_params)
27+
{
28+
$this->route_params = $route_params;
29+
}
30+
31+
/**
32+
* Magic method called when a non-existent or inaccessible method is
33+
* called on an object of this class. Used to execute before and after
34+
* filter methods on action methods. Action methods need to be named
35+
* with an "Action" suffix, e.g. indexAction, showAction etc.
36+
*
37+
* @param string $name Method name
38+
* @param array $args Arguments passed to the method
39+
*
40+
* @return void
41+
*/
42+
public function __call($name, $args)
43+
{
44+
$method = $name . 'Action';
45+
46+
if (method_exists($this, $method)) {
47+
if ($this->before() !== false) {
48+
call_user_func_array([$this, $method], $args);
49+
$this->after();
50+
}
51+
} else {
52+
throw new \Exception("Method $method not found in controller " . get_class($this));
53+
}
54+
}
55+
56+
/**
57+
* Before filter - called before an action method.
58+
*
59+
* @return void
60+
*/
61+
protected function before()
62+
{
63+
}
64+
65+
/**
66+
* After filter - called after an action method.
67+
*
68+
* @return void
69+
*/
70+
protected function after()
71+
{
72+
}
73+
}

Core/Error.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
/**
6+
* Error and exception handler
7+
*
8+
* PHP version 5.4
9+
*/
10+
class Error
11+
{
12+
13+
/**
14+
* Error handler. Convert all errors to Exceptions by throwing an ErrorException.
15+
*
16+
* @param int $level Error level
17+
* @param string $message Error message
18+
* @param string $file Filename the error was raised in
19+
* @param int $line Line number in the file
20+
*
21+
* @return void
22+
*/
23+
public static function errorHandler($level, $message, $file, $line)
24+
{
25+
if (error_reporting() !== 0) { // to keep the @ operator working
26+
throw new \ErrorException($message, 0, $level, $file, $line);
27+
}
28+
}
29+
30+
/**
31+
* Exception handler.
32+
*
33+
* @param Exception $exception The exception
34+
*
35+
* @return void
36+
*/
37+
public static function exceptionHandler($exception)
38+
{
39+
// Code is 404 (not found) or 500 (general error)
40+
$code = $exception->getCode();
41+
if ($code != 404) {
42+
$code = 500;
43+
}
44+
http_response_code($code);
45+
46+
if (\App\Config::SHOW_ERRORS) {
47+
echo "<h1>Fatal error</h1>";
48+
echo "<p>Uncaught exception: '" . get_class($exception) . "'</p>";
49+
echo "<p>Message: '" . $exception->getMessage() . "'</p>";
50+
echo "<p>Stack trace:<pre>" . $exception->getTraceAsString() . "</pre></p>";
51+
echo "<p>Thrown in '" . $exception->getFile() . "' on line " . $exception->getLine() . "</p>";
52+
} else {
53+
$log = dirname(__DIR__) . '/logs/' . date('Y-m-d') . '.txt';
54+
ini_set('error_log', $log);
55+
56+
$message = "Uncaught exception: '" . get_class($exception) . "'";
57+
$message .= " with message '" . $exception->getMessage() . "'";
58+
$message .= "\nStack trace: " . $exception->getTraceAsString();
59+
$message .= "\nThrown in '" . $exception->getFile() . "' on line " . $exception->getLine();
60+
61+
error_log($message);
62+
63+
View::renderTemplate("$code.html");
64+
}
65+
}
66+
}

Core/Model.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
use PDO;
6+
use App\Config;
7+
8+
/**
9+
* Base model
10+
*
11+
* PHP version 5.4
12+
*/
13+
abstract class Model
14+
{
15+
16+
/**
17+
* Get the PDO database connection
18+
*
19+
* @return mixed
20+
*/
21+
protected static function getDB()
22+
{
23+
static $db = null;
24+
25+
if ($db === null) {
26+
$dsn = 'mysql:host=' . Config::DB_HOST . ';dbname=' . Config::DB_NAME . ';charset=utf8';
27+
$db = new PDO($dsn, Config::DB_USER, Config::DB_PASSWORD);
28+
29+
// Throw an Exception when an error occurs
30+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
31+
}
32+
33+
return $db;
34+
}
35+
}

0 commit comments

Comments
 (0)