-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
12 changed files
with
226 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
tests/ export-ignore | ||
docs/ export-ignore -crlf -diff | ||
sample/ export-ignore |
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
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 @@ | ||
/cache/* |
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,3 @@ | ||
# Mastodon API SampleApp | ||
|
||
This directory is sample application for Mastodon API consumer. |
Empty file.
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,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(); | ||
}); |
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,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; | ||
} |
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,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]); | ||
} | ||
} |
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,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; | ||
} |
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 @@ | ||
# robots.txt |
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,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> |
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,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> |