Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cms entry point framework #5

Open
wants to merge 9 commits into
base: CMS-EntryPoint-Class
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions classes/EntryPoint.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
class EntryPoint {
private $route;
private $routes;

public function __construct($route) {
public function __construct($route, $routes) {
$this->route = $route;
$this->routes = $routes;
$this->checkUrl();
}

Expand All @@ -23,45 +25,9 @@ private function loadTemplate($templateFileName, $variables = []) {
return ob_get_clean();
}

private function callAction() {
include __DIR__ . '/../classes/DatabaseTable.php';
include __DIR__ . '/../includes/DatabaseConnection.php';

$jokesTable = new DatabaseTable($pdo, 'joke', 'id');
$authorsTable = new DatabaseTable($pdo, 'author', 'id');

if ($this->route === 'joke/list') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->list();
}
else if ($this->route === '') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->home();
}
else if ($this->route === 'joke/edit') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->edit();
}
else if ($this->route === 'joke/delete') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->delete();
}
else if ($this->route === 'register') {
include __DIR__ . '/../classes/controllers/RegisterController.php';
$controller = new RegisterController($authorsTable);
$page = $controller->showForm();
}

return $page;
}

public function run() {

$page = $this->callAction();
$page = $this->routes->callAction($this->route);

$title = $page['title'];

Expand Down
38 changes: 38 additions & 0 deletions classes/IjdbRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
class IjdbRoutes {
public function callAction($route) {
include __DIR__ . '/../classes/DatabaseTable.php';
include __DIR__ . '/../includes/DatabaseConnection.php';

$jokesTable = new DatabaseTable($pdo, 'joke', 'id');
$authorsTable = new DatabaseTable($pdo, 'author', 'id');

if ($route === 'joke/list') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->list();
}
else if ($route === '') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->home();
}
else if ($route === 'joke/edit') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->edit();
}
else if ($route === 'joke/delete') {
include __DIR__ . '/../classes/controllers/JokeController.php';
$controller = new JokeController($jokesTable, $authorsTable);
$page = $controller->delete();
}
else if ($route === 'register') {
include __DIR__ . '/../classes/controllers/RegisterController.php';
$controller = new RegisterController($authorsTable);
$page = $controller->showForm();
}

return $page;
}
}
6 changes: 0 additions & 6 deletions classes/controllers/JokeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public function list() {

$totalJokes = $this->jokesTable->total();

ob_start();

include __DIR__ . '/../../templates/';

$output = ob_get_clean();

return ['template' => 'jokes.html.php',
'title' => $title,
'variables' => [
Expand Down
3 changes: 2 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
try {
include __DIR__ . '/../classes/EntryPoint.php';
include __DIR__ . '/../classes/IjdbRoutes.php';

$route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/');

$entryPoint = new EntryPoint($route);
$entryPoint = new EntryPoint($route, new IjdbRoutes());
$entryPoint->run();
}
catch (PDOException $e) {
Expand Down
164 changes: 164 additions & 0 deletions public/samples/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@

<?php
try {
$pdo = new PDO('mysql:host=localhost;charset=utf8', 'homestead', 'secret');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch (PDOException $e) {
echo '<h3>Could not connect to database. Did you delete the `homestead` user or change it\'s password?</h3>';
echo '<p>' . $e . '</p>';
die;
}


try {
//sample user might not exist so the query may throw an exception, that's fine, ignore it.
try {
//Drop the user, there's a chance the password has been changed
$pdo->query('DROP USER \'ijdb_sample\'@\'localhost\'');
}
catch (PDOException $e) {}

//Create the user for the sample code to use
$pdo->query('CREATE USER \'ijdb_sample\'@\'localhost\' IDENTIFIED BY \'mypassword\'');

//Drop the database, only one sample should be used at once.

$pdo->query('DROP DATABASE IF EXISTS ijdb_sample');


$pdo->query('CREATE DATABASE ijdb_sample');
$pdo->query('GRANT ALL PRIVILEGES ON ijdb_sample.* To \'ijdb_sample\'@\'localhost\'');
$pdo->query('FLUSH PRIVILEGES');
$pdo->query('USE ijdb_sample');

if (file_exists('../../database.sql')) {
$pdo->exec(file_get_contents('../../database.sql'));
}

}
catch (PDOException $e) {
echo 'Could not create sample database/user';
echo $e->getMessage();
}


exec('git status', $output);
$branchName = str_replace('On branch ', '', $output[0]);

if (isset($_GET['branch'])) {
exec('git status', $status);
$status = implode("\n", $status);
if (strpos($status, 'nothing to commit') == false) {



$parts = explode('_Modified', $branchName);
$newBranchName = $parts[0] . '_Modified-' . date('Y-m-d-H.i.s');


exec('git checkout -b ' . $newBranchName . ' 2>&1', $z);

exec('git add -A 2>&1', $x);
exec('git commit -m "user modified sample" 2>&1', $y);

var_dump($z);
var_dump($y);
var_dump($x);
}
exec('git checkout "' . $_GET['branch'] . '"', $n);
$branchName = $_GET['branch'];
}

if (!isset($branchName)) {
exec('git status', $output);
$branchName = str_replace('On branch ', '', $output[0]);
}


?><!doctype html>
<html>
<head>
<title><?= $branchName; ?> - PHP Nove to Ninja sample code</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
a {text-decoration: none}
body {padding: 0; margin: 0 background-color: #f7f7f7; font-family: 'Roboto',arial,helvetica, sans-serif}
h1 {padding: 1em; display: block; text-align: center}
.files {display: block; background-color:#3a3a3a; padding: 10px; list-style-type: none; margin: 0}
.files a, .files a:visited {color: white; font-size: 2em}
li {padding: 1em; border-top: 0.2em solid #fff; overflow: auto}
li:first-child {border: 0}
.files a:hover {color: #ddd}
code {margin-top: 1em; background-color: #efefef; display: block; clear: both; padding: 0.5em; overflow-x: auto}


.branches {list-style-type: none; background-color: #ccc; padding: 0; font-size: 1.3em}
.branches a, .branches a:visited {color: #000}
h2 {margin-top: 2em}
.current {font-weight: bold; background-color: #333;}
.current a, .current a:visited {color: white}
</style>
</head>
<body>




<h1>PHP Novice to Ninja sample code</h1>

<p>Click on a file to view in your browser</p>

<ul class="files">
<?php

foreach (new DirectoryIterator('../') as $file) {
if ($file->isDot()) continue;
if ($file->getFileName() == 'samples') continue;

$code = file_get_contents('../' . $file->getFileName());
echo '<li><a href="/' . $file->getFileName() . '">' . $file->getFileName() . '</a>';

echo highlight_string($code, true);
echo '</li>';
}

?>
</ul>


<h2>View a different sample</h2>

<ul class="branches">
<?php


exec('git branch -a', $branches);

$branchList = [];

foreach ($branches as $branch) {


$branch = trim($branch, " \t*");
$branch = str_replace('origin/', '', $branch);
$branch = str_replace('remotes/', '', $branch);

$branchList[$branch] = $branch;

}

foreach ($branchList as $branch) {
$class =$branch == $branchName ? 'current' : '';

if ($branch == 'master') continue;
echo '<li class="' .$class . '"><a href="' . $_SERVER['PHP_SELF'] . '?branch=' . $branch . '">' . $branch . '</a></li>';
}



?>
</ul>
</body>
</html>