Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
victoryoalli committed Jun 10, 2023
0 parents commit 5147710
Show file tree
Hide file tree
Showing 24 changed files with 474 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
32 changes: 32 additions & 0 deletions app/Controllers/PagesController.php
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');
}
}
30 changes: 30 additions & 0 deletions app/Controllers/UsersController.php
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');
}
}
38 changes: 38 additions & 0 deletions app/Core/App.php
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];
}
}
28 changes: 28 additions & 0 deletions app/Core/Database/Connection.php
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());
}
}
}
63 changes: 63 additions & 0 deletions app/Core/Database/QueryBuilder.php
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) {
//
}
}
}
28 changes: 28 additions & 0 deletions app/Core/Request.php
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'];
}
}
89 changes: 89 additions & 0 deletions app/Core/Router.php
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();
}
}
10 changes: 10 additions & 0 deletions app/Core/bootstrap.php
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'])
));
30 changes: 30 additions & 0 deletions app/Core/helpers.php
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";
}
8 changes: 8 additions & 0 deletions app/routes.php
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');
5 changes: 5 additions & 0 deletions app/views/about-culture.view.php
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'); ?>
Loading

0 comments on commit 5147710

Please sign in to comment.