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

Add PHP implementation. #11

Open
wants to merge 3 commits into
base: master
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
14 changes: 14 additions & 0 deletions todos/php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# TODO-MVP in PHP

This implementation is written in [PHP](https://www.php.net).

- PHP version 7.3+.

HTML copied from Ruby implementation.

## Instructions

1. Clone the repository
2. Requires PHP installed.
3. From the php/ directory run: `php -S localhost:8000`
4. Open browser to http://localhost:8000
33 changes: 33 additions & 0 deletions todos/php/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Project-specific auto-loading implementation.
*
* After registering this autoload function with SPL, the namespaces on the left of the loaders array will load classes found in the paths on the right.
*
* @param string $class The fully-qualified class name.
* @return void
*/

spl_autoload_register(function ($class) {
$loaders = [
'Todo\\PHP\\' => '/classes/'
];

foreach($loaders as $prefix => $base_dir){
$len = strlen($prefix);
if(strncmp($prefix, $class,$len) !== 0){
continue;
}
$relative_class = substr($class, $len);

$file = __DIR__ . $base_dir .
str_replace('\\', '/',
$relative_class) .
'.php';

if (file_exists($file)) {
require $file;
return;
}
} //end foreach
});
75 changes: 75 additions & 0 deletions todos/php/classes/controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace Todo\PHP;

/* A very basic controller class.
*/

class controller
{
private $requestPost;
private $requestGet;

/**
* Intialize with our existing array of todo items.
*
* @param array $list
*/
public function __construct(array $post = [], array $get = [])
{
$this->requestPost = $post;
$this->requestGet = $get;
}

/**
* Route: / or /index
* If a http POST has been received, add the item, otherwise list items.
*
* @param todo $todo
* @return void
*/
public function index(todo &$todo){
if (isset($this->requestPost['item'])) {
$todo->addItem($this->requestPost['item']);
}
return 'page';
}

/**
* Route: /done
*
* @param todo $todo
* @return void
*/
public function done(todo &$todo){
if (isset($this->requestPost['item'])) {
$todo->setItemDone($this->requestPost['item']);
}
return 'page';
}

/**
* Route: /notdone
*
* @param todo $todo
* @return void
*/
public function notdone(todo &$todo){
if (isset($this->requestPost['item'])) {
$todo->setItemNotDone($this->requestPost['item']);
}
return 'page';
}

/**
* Route: /delete
*
* @param todo $todo
* @return string
*/
public function delete(todo &$todo){
if (isset($this->requestPost['item'])) {
$todo->delete($this->requestPost['item']);
}
return 'page';
}
}
86 changes: 86 additions & 0 deletions todos/php/classes/todo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
namespace Todo\PHP;

/* A minimum viable ToDo application in PHP.
* No user sessions or saving.
*/

class todo
{
private $items;

/**
* Intialize with our existing array of todo items.
*
* @param array $list
*/
public function __construct(array $list = [])
{
$this->items = $list;
}

/**
* Add a new todo item, default to not done.
*
* @param string $name
* @return void
*/
public function addItem(string $name)
{
$this->items[] = array('name' => $name, 'done' => 0);
}

/**
* Delete an item at the given number.
*
* @param int $num
* @return void
*/
public function delete(int $num)
{
unset($this->items[$num]);
}

/**
* Fetch our current list of todo items as an array.
*
* @return array
*/
public function getItems(): array
{
return $this->items;
}

/**
* Get our array of current items as a JSON string.
* For passing to Javascript or persistence in hidden form fields.
*
* @return string
*/
public function getItemsJSON(): string
{
return json_encode($this->items);
}

/**
* Mark a todo item as complete.
*
* @param integer $num
* @return void
*/
public function setItemDone(int $num)
{
$this->items[$num]['done'] = 1;
}

/**
* Mark a todo item as incomplete.
*
* @param integer $num
* @return void
*/
public function setItemNotDone(int $num)
{
$this->items[$num]['done'] = 0;
}
}
46 changes: 46 additions & 0 deletions todos/php/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/* Web application entry point and bootstrapping.
*/

use Todo\PHP\todo;
use Todo\PHP\controller;

require_once('autoload.php');

$controller = new controller($_POST, $_GET);
$template = '';

// Grab our passed URL parameters (pretty URLs).
$url = $_GET['url'] ?? $_SERVER['PATH_INFO'] ?? "";

// Parse the controller action out of the url (first part before first slash). The rest is the 'parameters'.
$parameters = explode("/", ltrim($url, '/'));
$action = $parameters[0];
$parameters = array_shift($parameters);
if(empty($action)) $action = 'index';


// Initialize todo list object.
if (empty($_POST) || !array_key_exists('current-list', $_POST)) {
$todoList = new todo(); // No persistence, so we start a new instance.
} else {
$todoList = new todo(json_decode($_POST['current-list'], true));
}

// Very basic routing.
if ((int)method_exists($controller, $action)) {
// Call controller method and inject $todoList object.
// In a full application we'd also pass the $parameters values to the controller actions.
$template = call_user_func_array(array($controller, $action), array(&$todoList));
} else {
http_response_code(404);
echo("404 Page not found. <a href='/'>Return home.</a>");
exit();
}

// Set up values for our 'template':
$tpl_todos = $todoList->getItems();
$tpl_currentList = htmlspecialchars($todoList->getItemsJSON());

// Include our html page template, which uses the above values.
require_once($template.'.tpl.php');
89 changes: 89 additions & 0 deletions todos/php/page.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html>
<head>
<title>Todo MVP</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/static/todo.css">
</head>
<body>
<h1>Todo MVP</h1>
<section class="new-todo">
<form method="POST" action="/">
<input type="text"
id="new-item"
name="item"
pattern=".{3,}"
required="required"
aria-label="Write a new todo item"
title="3 characters minimum" />
<input type="hidden"
name="current-list"
value="<?=$tpl_currentList;?>" />
<input type="submit"
value="Add new item"
id="add-new-item"
name="add-new-item" />
</form>
</section>

<section class="items">
<h2>Todo list</h2>
<ul><?php
foreach($tpl_todos as $num => $todo){
if ($todo['done']){ ?>
<li class="todo done">
<span class="item-name">
<s><?=$todo['name'];?></s>
</span>
<form method="post" action="/notdone">
<input type="hidden" name="item" value="<?=$num;?>"/>
<input type="hidden"
name="current-list"
value="<?=$tpl_currentList;?>" />
<input class="uncomplete"
type="submit"
name="mark-not-done"
value="Mark not done '<?=$todo['name'];?>'" />
</form>
<form method="post" action="/delete">
<input type="hidden" name="item" value="<?=$num;?>"/>
<input type="hidden"
name="current-list"
value="<?=$tpl_currentList;?>" />
<input class="delete"
type="submit"
name="delete"
value="Delete '<?=$todo['name'];?>'" />
</form>
</li>
<?php }else { ?>
<li class="todo">
<span class="item-name">
<?=$todo['name'];?>
</span>
<form method="post" action="/done">
<input type="hidden" name="item" value="<?=$num;?>"/>
<input type="hidden"
name="current-list"
value="<?=$tpl_currentList;?>" />
<input class="complete"
type="submit"
name="mark-done"
value="Mark done '<?=$todo['name'];?>'" />
</form>
<form method="post" action="/delete">
<input type="hidden" name="item" value="<?=$num;?>"/>
<input type="hidden"
name="current-list"
value="<?=$tpl_currentList;?>" />
<input class="delete"
type="submit"
name="delete"
value="Delete '<?=$todo['name'];?>'" />
</form>
</li>
<?php }
} ?></ul>
</section>
</body>
</html>
1 change: 1 addition & 0 deletions todos/php/static/check.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions todos/php/static/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added todos/php/static/tick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading