Skip to content

Commit

Permalink
Use PSR-4 and limit to PHP 5.4 as a minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Jul 20, 2015
1 parent d178cfe commit 7cdc97b
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 303 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
}
],
"require": {
"php": ">=5.3",
"php": ">=5.4",
"erusev/parsedown": "~1.0"
},
"autoload": {
"psr-4": {"Todaymade\\Daux\\": "libs/"}
}
}
6 changes: 3 additions & 3 deletions generate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once("libs/daux.php");
require_once("vendor/autoload.php");

/*
Expand Down Expand Up @@ -68,5 +68,5 @@
else $Daux = new \Todaymade\Daux\Daux();
$Daux->initialize();
if (isset($argv[2])) $Daux->generate_static($argv[2]);
else $Daux->generate_static();
?>
else $Daux->generate_static();
?>
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once("libs/daux.php");
require_once("vendor/autoload.php");

/*
Expand Down Expand Up @@ -68,4 +68,4 @@
$Daux->initialize();
$page = $Daux->handle_request($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST);
$page->display();
?>
?>
10 changes: 2 additions & 8 deletions libs/daux.php → libs/Daux.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
namespace Todaymade\Daux;
require_once(dirname(__FILE__) . '/../vendor/autoload.php');
require_once('daux_directory.php');
require_once('daux_helper.php');
require_once('daux_page.php');

<?php namespace Todaymade\Daux;

class Daux
{
Expand Down Expand Up @@ -154,7 +148,7 @@ private function recursive_generate_static($tree, $output_dir, $params, $base_ur
$params['image'] = str_replace('<base_url>', $base_url, $params['image']);
if ($base_url !== '') $params['entry_page'] = $tree->first_page;
foreach ($tree->value as $key => $node) {
if ($node->type === Directory_Entry::DIRECTORY_TYPE) {
if ($node->type === Entry::DIRECTORY_TYPE) {
$new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
@mkdir($new_output_dir);
$this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url);
Expand Down
71 changes: 4 additions & 67 deletions libs/daux_helper.php → libs/DauxHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
namespace Todaymade\Daux;
<?php namespace Todaymade\Daux;

class DauxHelper {

Expand Down Expand Up @@ -213,7 +212,7 @@ public static function piwik_analytics($analytics_url, $analytics_id) {

private static function directory_tree_builder($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) {
if ($dh = opendir($dir)) {
$node = new Directory_Entry($dir, $parents);
$node = new Entry($dir, $parents);
$new_parents = $parents;
if (is_null($new_parents)) $new_parents = array();
else $new_parents[] = $node;
Expand All @@ -227,10 +226,10 @@ private static function directory_tree_builder($dir, $ignore, $mode = Daux::LIVE
if (is_dir($path)) $entry = static::directory_tree_builder($path, $ignore, $mode, $new_parents);
else if (in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS))
{
$entry = new Directory_Entry($path, $new_parents);
$entry = new Entry($path, $new_parents);
if ($mode === Daux::STATIC_MODE) $entry->uri .= '.html';
}
if ($entry instanceof Directory_Entry) $node->value[$entry->uri] = $entry;
if ($entry instanceof Entry) $node->value[$entry->uri] = $entry;
}
$node->sort();
$node->first_page = $node->get_first_page();
Expand Down Expand Up @@ -299,65 +298,3 @@ private static function copy_recursive($src,$dst) {
}

}

if (!function_exists('http_response_code')) {
function http_response_code($code = NULL) {

if ($code !== NULL) {

switch ($code) {
case 100: $text = 'Continue'; break;
case 101: $text = 'Switching Protocols'; break;
case 200: $text = 'OK'; break;
case 201: $text = 'Created'; break;
case 202: $text = 'Accepted'; break;
case 203: $text = 'Non-Authoritative Information'; break;
case 204: $text = 'No Content'; break;
case 205: $text = 'Reset Content'; break;
case 206: $text = 'Partial Content'; break;
case 300: $text = 'Multiple Choices'; break;
case 301: $text = 'Moved Permanently'; break;
case 302: $text = 'Moved Temporarily'; break;
case 303: $text = 'See Other'; break;
case 304: $text = 'Not Modified'; break;
case 305: $text = 'Use Proxy'; break;
case 400: $text = 'Bad Request'; break;
case 401: $text = 'Unauthorized'; break;
case 402: $text = 'Payment Required'; break;
case 403: $text = 'Forbidden'; break;
case 404: $text = 'Not Found'; break;
case 405: $text = 'Method Not Allowed'; break;
case 406: $text = 'Not Acceptable'; break;
case 407: $text = 'Proxy Authentication Required'; break;
case 408: $text = 'Request Time-out'; break;
case 409: $text = 'Conflict'; break;
case 410: $text = 'Gone'; break;
case 411: $text = 'Length Required'; break;
case 412: $text = 'Precondition Failed'; break;
case 413: $text = 'Request Entity Too Large'; break;
case 414: $text = 'Request-URI Too Large'; break;
case 415: $text = 'Unsupported Media Type'; break;
case 500: $text = 'Internal Server Error'; break;
case 501: $text = 'Not Implemented'; break;
case 502: $text = 'Bad Gateway'; break;
case 503: $text = 'Service Unavailable'; break;
case 504: $text = 'Gateway Time-out'; break;
case 505: $text = 'HTTP Version not supported'; break;
default:
exit('Unknown http status code "' . htmlentities($code) . '"');
break;
}

$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' ' . $code . ' ' . $text);
$GLOBALS['http_response_code'] = $code;

} else {
$code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200);
}
return $code;
}
}


?>
10 changes: 4 additions & 6 deletions libs/daux_directory.php → libs/Entry.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
namespace Todaymade\Daux;
class Directory_Entry
<?php namespace Todaymade\Daux;
class Entry
{
const FILE_TYPE = 'FILE_TYPE';
const DIRECTORY_TYPE = 'DIRECTORY_TYPE';
Expand All @@ -26,10 +25,10 @@ function __construct($path = '', $parents = array()) {
$this->uri = DauxHelper::get_url_from_filename($this->name);
$this->index_page = false;
if (is_dir($path)) {
$this->type = Directory_Entry::DIRECTORY_TYPE;
$this->type = Entry::DIRECTORY_TYPE;
$this->value = array();
} else {
$this->type = Directory_Entry::FILE_TYPE;
$this->type = Entry::FILE_TYPE;
$this->value = $this->uri;
}
}
Expand Down Expand Up @@ -125,4 +124,3 @@ private function compare_directory_entries($a, $b) {
}
}
}
?>
48 changes: 48 additions & 0 deletions libs/ErrorPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php namespace Todaymade\Daux;

class ErrorPage extends SimplePage
{
const NORMAL_ERROR_TYPE = 'NORMAL_ERROR';
const MISSING_PAGE_ERROR_TYPE = 'MISSING_PAGE_ERROR';
const FATAL_ERROR_TYPE = 'FATAL_ERROR';

private $params;
private $type;
private static $template;

public function __construct($title, $content, $params) {
parent::__construct($title, $content);
$this->params = $params;
$this->type = $params['error_type'];
}

public function display() {
http_response_code($this->type === static::MISSING_PAGE_ERROR_TYPE ? 404 : 500);
parent::display();
}

public function get_page_content() {
if ($this->type !== static::FATAL_ERROR_TYPE && is_null(static::$template)) {
include_once($this->params['theme']['error-template']);
static::$template = new Template();
}

if (is_null($this->html)) {
$this->html = $this->generate_page();
}

return $this->html;
}

public function generate_page() {
if ($this->type === static::FATAL_ERROR_TYPE) return $this->content;
$params = $this->params;
$page['title'] = $this->title;
$page['theme'] = $params['theme'];
$page['content'] = $this->content;
$page['google_analytics'] = $params['google_analytics'];
$page['piwik_analytics'] = $params['piwik_analytics'];

return static::$template->get_content($page, $params);
}
}
111 changes: 111 additions & 0 deletions libs/MarkdownPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php namespace Todaymade\Daux;

class MarkdownPage extends SimplePage
{
private $filename;
private $params;
private $language;
private $mtime;
private $homepage;
private $breadcrumb_trail;
private static $template;

public function __construct() {

}

// For Future Expansion
public static function fromFile($file, $params) {
$instance = new self();
$instance->initialize_from_file($file, $params);
return $instance;
}

private function initialize_from_file($file, $params) {
$this->title = $file->title;
$this->filename = $file->name;
$this->path = $file->local_path;
$this->mtime = $file->last_modified;
$this->params = $params;

if ($this->title === 'index') {
$this->homepage = ($this->filename === '_index');
$minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1;
if (count($file->parents) >= $minimum_parent_dir_size) {
$parent = end($file->parents);
$this->title = $parent->title;
} else $this->title = $params['title'];
} else {
$this->homepage = false;
}
if ($params['breadcrumbs'])
$this->breadcrumb_trail = $this->build_breadcrumb_trail($file->parents, $params['multilanguage']);
$this->language = '';
if ($params['multilanguage'] && !empty($file->parents)) {
reset($file->parents);
$language_dir = current($file->parents);
$this->language = $language_dir->name;
}
if (is_null(static::$template)) {
include_once($params['theme']['template']);
static::$template = new Template();
}
}

private function build_breadcrumb_trail($parents, $multilanguage) {
if ($multilanguage && !empty($parents)) $parents = array_splice($parents, 1);
$breadcrumb_trail = array();
if (!empty($parents)) {
foreach ($parents as $node) {
$breadcrumb_trail[$node->title] = $node->get_url();
}
}
return $breadcrumb_trail;
}

public function get_page_content() {
if (is_null($this->html)) {
$this->content = file_get_contents($this->path);
$this->html = $this->generate_page();
}

return $this->html;
}

private function generate_page() {
$params = $this->params;
$Parsedown = new \Parsedown();
if ($params['request'] === $params['index_key']) {
if ($params['multilanguage']) {
foreach ($params['languages'] as $key => $name) {
$entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->get_url());
}
} else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri);
} else if ($params['file_uri'] === 'index')
$entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'].
$params['entry_page']->get_url());
$page['entry_page'] = (isset($entry_page)) ? $entry_page : null;

$page['homepage'] = $this->homepage;
$page['title'] = $this->title;
$page['tagline'] = $params['tagline'];
$page['author'] = $params['author'];
$page['filename'] = $this->filename;
if ($page['breadcrumbs'] = $params['breadcrumbs']) {
$page['breadcrumb_trail'] = $this->breadcrumb_trail;
$page['breadcrumb_separator'] = $params['breadcrumb_separator'];
}
$page['language'] = $this->language;
$page['path'] = $this->path;
$page['request'] = utf8_encode($params['request']);
$page['theme'] = $params['theme'];
$page['modified_time'] = filemtime($this->path);
$page['markdown'] = $this->content;
$page['content'] = $Parsedown->text($this->content);
$page['file_editor'] = $params['file_editor'];
$page['google_analytics'] = $params['google_analytics'];
$page['piwik_analytics'] = $params['piwik_analytics'];

return static::$template->get_content($page, $params);
}
}
7 changes: 7 additions & 0 deletions libs/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Todaymade\Daux;

interface Page
{
function get_page_content();
function display();
}
34 changes: 34 additions & 0 deletions libs/SimplePage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace Todaymade\Daux;

class SimplePage implements Page
{
protected $title;
protected $content;
protected $html = null;

public function __construct($title, $content) {
$this->initialize_page($title, $content);
}

public function initialize_page($title, $content) {
$this->title = $title;
$this->content = $content;
}

public function display() {
header('Content-type: text/html; charset=utf-8');
echo $this->get_page_content();
}

public function get_page_content() {
if (is_null($this->html)) {
$this->html = $this->generate_page();
}

return $this->html;
}

private function generate_page() {
return $this->content;
}
}
Loading

0 comments on commit 7cdc97b

Please sign in to comment.