forked from victoryoalli/miniphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5147710
Showing
24 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
# 4 space indentation | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
# Matches multiple files with brace expansion notation | ||
# Set default charset | ||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.{yml,yaml}] | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
class PagesController | ||
{ | ||
/** | ||
* Show the home page. | ||
*/ | ||
public function home() | ||
{ | ||
return view('index'); | ||
} | ||
|
||
/** | ||
* Show the about page. | ||
*/ | ||
public function about() | ||
{ | ||
$company = 'Laracasts'; | ||
|
||
return view('about', ['company' => $company]); | ||
} | ||
|
||
/** | ||
* Show the contact page. | ||
*/ | ||
public function contact() | ||
{ | ||
return view('contact'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Core\App; | ||
|
||
class UsersController | ||
{ | ||
/** | ||
* Show all users. | ||
*/ | ||
public function index() | ||
{ | ||
$users = App::get('database')->selectAll('users'); | ||
|
||
return view('users', compact('users')); | ||
} | ||
|
||
/** | ||
* Store a new user in the database. | ||
*/ | ||
public function store() | ||
{ | ||
App::get('database')->insert('users', [ | ||
'name' => $_POST['name'] | ||
]); | ||
|
||
return redirect('users'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
class App | ||
{ | ||
/** | ||
* All registered keys. | ||
* | ||
* @var array | ||
*/ | ||
protected static $registry = []; | ||
|
||
/** | ||
* Bind a new key/value into the container. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
*/ | ||
public static function bind($key, $value) | ||
{ | ||
static::$registry[$key] = $value; | ||
} | ||
|
||
/** | ||
* Retrieve a value from the registry. | ||
* | ||
* @param string $key | ||
*/ | ||
public static function get($key) | ||
{ | ||
if (! array_key_exists($key, static::$registry)) { | ||
throw new \Exception("No {$key} is bound in the container."); | ||
} | ||
|
||
return static::$registry[$key]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Core\Database; | ||
|
||
use PDO; | ||
use PDOException; | ||
|
||
class Connection | ||
{ | ||
/** | ||
* Create a new PDO connection. | ||
* | ||
* @param array $config | ||
*/ | ||
public static function make($config) | ||
{ | ||
try { | ||
return new PDO( | ||
$config['connection'].';dbname='.$config['name'], | ||
$config['username'], | ||
$config['password'], | ||
$config['options'] | ||
); | ||
} catch (PDOException $e) { | ||
die($e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Core\Database; | ||
|
||
use PDO; | ||
|
||
class QueryBuilder | ||
{ | ||
/** | ||
* The PDO instance. | ||
* | ||
* @var PDO | ||
*/ | ||
protected $pdo; | ||
|
||
/** | ||
* Create a new QueryBuilder instance. | ||
* | ||
* @param PDO $pdo | ||
*/ | ||
public function __construct($pdo) | ||
{ | ||
$this->pdo = $pdo; | ||
} | ||
|
||
/** | ||
* Select all records from a database table. | ||
* | ||
* @param string $table | ||
*/ | ||
public function selectAll($table) | ||
{ | ||
$statement = $this->pdo->prepare("select * from {$table}"); | ||
|
||
$statement->execute(); | ||
|
||
return $statement->fetchAll(PDO::FETCH_CLASS); | ||
} | ||
|
||
/** | ||
* Insert a record into a table. | ||
* | ||
* @param string $table | ||
* @param array $parameters | ||
*/ | ||
public function insert($table, $parameters) | ||
{ | ||
$sql = sprintf( | ||
'insert into %s (%s) values (%s)', | ||
$table, | ||
implode(', ', array_keys($parameters)), | ||
':' . implode(', :', array_keys($parameters)) | ||
); | ||
|
||
try { | ||
$statement = $this->pdo->prepare($sql); | ||
|
||
$statement->execute($parameters); | ||
} catch (\Exception $e) { | ||
// | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
class Request | ||
{ | ||
/** | ||
* Fetch the request URI. | ||
* | ||
* @return string | ||
*/ | ||
public static function uri() | ||
{ | ||
return trim( | ||
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/' | ||
); | ||
} | ||
|
||
/** | ||
* Fetch the request method. | ||
* | ||
* @return string | ||
*/ | ||
public static function method() | ||
{ | ||
return $_SERVER['REQUEST_METHOD']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
class Router | ||
{ | ||
/** | ||
* All registered routes. | ||
* | ||
* @var array | ||
*/ | ||
public $routes = [ | ||
'GET' => [], | ||
'POST' => [] | ||
]; | ||
|
||
/** | ||
* Load a user's routes file. | ||
* | ||
* @param string $file | ||
*/ | ||
public static function load($file) | ||
{ | ||
$router = new static; | ||
|
||
require __DIR__.'/../../'.$file; | ||
|
||
return $router; | ||
} | ||
|
||
/** | ||
* Register a GET route. | ||
* | ||
* @param string $uri | ||
* @param string $controller | ||
*/ | ||
public function get($uri, $controller) | ||
{ | ||
$this->routes['GET'][$uri] = $controller; | ||
} | ||
|
||
/** | ||
* Register a POST route. | ||
* | ||
* @param string $uri | ||
* @param string $controller | ||
*/ | ||
public function post($uri, $controller) | ||
{ | ||
$this->routes['POST'][$uri] = $controller; | ||
} | ||
|
||
/** | ||
* Load the requested URI's associated controller method. | ||
* | ||
* @param string $uri | ||
* @param string $requestType | ||
*/ | ||
public function direct($uri, $requestType) | ||
{ | ||
if (array_key_exists($uri, $this->routes[$requestType])) { | ||
return $this->callAction( | ||
...explode('@', $this->routes[$requestType][$uri]) | ||
); | ||
} | ||
|
||
throw new Exception('No route defined for this URI.'); | ||
} | ||
|
||
/** | ||
* Load and call the relevant controller action. | ||
* | ||
* @param string $controller | ||
* @param string $action | ||
*/ | ||
protected function callAction($controller, $action) | ||
{ | ||
$controller = "App\\Controllers\\{$controller}"; | ||
$controller = new $controller; | ||
|
||
if (! method_exists($controller, $action)) { | ||
throw new Exception( | ||
"{$controller} does not respond to the {$action} action." | ||
); | ||
} | ||
|
||
return $controller->$action(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use App\Core\App; | ||
use App\Core\Database\{QueryBuilder, Connection}; | ||
|
||
App::bind('config', config('database')); | ||
|
||
App::bind('database', new QueryBuilder( | ||
Connection::make(App::get('config')['database']) | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/** | ||
* Require a view. | ||
* | ||
* @param string $name | ||
* @param array $data | ||
*/ | ||
function view($name, $data = []) | ||
{ | ||
extract($data); | ||
|
||
return require __DIR__."/../../app/views/{$name}.view.php"; | ||
} | ||
|
||
/** | ||
* Redirect to a new page. | ||
* | ||
* @param string $path | ||
*/ | ||
function redirect($path) | ||
{ | ||
header("Location: /{$path}"); | ||
} | ||
|
||
|
||
function config($key) | ||
{ | ||
return require __DIR__.'/../../config/'."{$key}.php"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
$router->get('', 'PagesController@home'); | ||
$router->get('about', 'PagesController@about'); | ||
$router->get('contact', 'PagesController@contact'); | ||
|
||
$router->get('users', 'UsersController@index'); | ||
$router->post('users', 'UsersController@store'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php require('partials/head.php'); ?> | ||
|
||
<h1>Our Culture at <?= $name; ?></h1> | ||
|
||
<?php require('partials/footer.php'); ?> |
Oops, something went wrong.