Skip to content

Commit

Permalink
Add sample dir
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed May 22, 2017
1 parent c348e17 commit 63401a6
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/ export-ignore
docs/ export-ignore -crlf -diff
sample/ export-ignore
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"paragonie/random_compat": "^2.0",
"symfony/polyfill-php70": "^1.3",
"symfony/var-dumper": "^3.2",
"filp/whoops": "^2.1",
"zonuexe/simple-routing": "^0.5.3",
"phpunit/phpunit": "^4.8"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/cache/*
3 changes: 3 additions & 0 deletions sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Mastodon API SampleApp

This directory is sample application for Mastodon API consumer.
Empty file added sample/cache/session/.keep
Empty file.
26 changes: 26 additions & 0 deletions sample/inc/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Template for all contents
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
*/

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/variables.php';

call_user_func(function() {
error_reporting(-1);

if ($_SERVER['SERVER_NAME'] === 'localhost') {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
}

session_save_path(realpath(__DIR__ . '/../cache/session/'));
session_start();
});
55 changes: 55 additions & 0 deletions sample/inc/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Helper functions for SampleApp
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
*/

use Teto\Routing\Router;

/**
* @param string $input
* @return string
*/
function h($input)
{
return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
}

/**
* @param string $tpl_name
* @param array $data
* @return string
*/
function view($tpl_name, array $data = [])
{
$main_tpl = __DIR__ . "/../view/{$tpl_name}.tpl.php";
if (!file_exists($main_tpl)) {
throw new \RuntimeException("Template file not exists: {$tpl_name}");
}

ob_start();
$var = new variables($data);
include __DIR__ . '/../view/body.tpl.php';

return ob_get_clean();
}

/**
* @param Router $router
* @return Router
*/
function router(Router $router = null)
{
/** @var Router $cache */
static $cache;

if ($router !== null) {
$cache = $router;
}

return $cache;
}
65 changes: 65 additions & 0 deletions sample/inc/variables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* Variables container
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
*/
final class variables implements \ArrayAccess
{
/** @var array */
private $data;

public function __construct(array $data)
{
$this->data = $data;
}

/**
* @param string $offset
* @param array $option
* @return mixed
*/
public function get($offset, array $option)
{
return isset($this[$offset]) ? $this[$offset] : $option['default'];
}

/**
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->data);
}

/**
* @param string $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->data[$offset];
}

/**
* @param string $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
}
42 changes: 42 additions & 0 deletions sample/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Mastodon SampleApp router and core application file
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
*/

use Teto\Routing\Action;

require __DIR__ . '/../inc/bootstrap.php';

if (php_sapi_name() === 'cli-server') {
if (strpos($_SERVER['REQUEST_URI'], '..') !== false) {
http_response_code(404);
return true;
}
$path = __DIR__ . implode(DIRECTORY_SEPARATOR, explode('/', $_SERVER['REQUEST_URI']));
if (is_file($path)) {
return false;
}
}

$routes = [];
$routes['index'] = ['GET', '/', function (Action $action) {
return [200, [], view('index')];
}];

router($router = new \Teto\Routing\Router($routes));
$action = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
list($status, $headers, $content) = call_user_func($action->value, $action);

http_response_code($status);
foreach ($headers as $name => $header) {
header("{$name}:{$header}");
}

if ($content !== null) {
echo (string)$content;
}
1 change: 1 addition & 0 deletions sample/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# robots.txt
19 changes: 19 additions & 0 deletions sample/view/body.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Template for all contents
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
*/
/** @var $var variables */
?>
<!DOCTYPE html>
<html>
<head>
<title><?= h($var->get('title', ['default' => 'PHP Mastodon Sample App'])) ?></title>
</head>
<body>
<?php isset($main_tpl) && include $main_tpl; ?>
</body>
</html>
11 changes: 11 additions & 0 deletions sample/view/index.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Template for index
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2017 Baguette HQ
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
*/
/** @var $var variables */
?>
<h1>Mastodon Sample App</h1>

0 comments on commit 63401a6

Please sign in to comment.