From 7cdc97b58afe252d299074240e085ec095c25e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Tue, 21 Apr 2015 17:11:43 +0200 Subject: [PATCH] Use PSR-4 and limit to PHP 5.4 as a minimum --- composer.json | 5 +- generate.php | 6 +- index.php | 4 +- libs/{daux.php => Daux.php} | 10 +- libs/{daux_helper.php => DauxHelper.php} | 71 +------- libs/{daux_directory.php => Entry.php} | 10 +- libs/ErrorPage.php | 48 ++++++ libs/MarkdownPage.php | 111 +++++++++++++ libs/Page.php | 7 + libs/SimplePage.php | 34 ++++ libs/daux_page.php | 200 ----------------------- templates/default/default.tpl | 4 +- templates/default/error.tpl | 4 +- vendor/composer/ClassLoader.php | 67 +++++++- vendor/composer/autoload_psr4.php | 1 + vendor/composer/autoload_real.php | 3 - 16 files changed, 282 insertions(+), 303 deletions(-) rename libs/{daux.php => Daux.php} (98%) rename libs/{daux_helper.php => DauxHelper.php} (81%) rename libs/{daux_directory.php => Entry.php} (96%) create mode 100644 libs/ErrorPage.php create mode 100644 libs/MarkdownPage.php create mode 100644 libs/Page.php create mode 100644 libs/SimplePage.php delete mode 100644 libs/daux_page.php diff --git a/composer.json b/composer.json index 933b1b1..e561988 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,10 @@ } ], "require": { - "php": ">=5.3", + "php": ">=5.4", "erusev/parsedown": "~1.0" + }, + "autoload": { + "psr-4": {"Todaymade\\Daux\\": "libs/"} } } diff --git a/generate.php b/generate.php index ab000c2..13afe2d 100644 --- a/generate.php +++ b/generate.php @@ -1,5 +1,5 @@ initialize(); if (isset($argv[2])) $Daux->generate_static($argv[2]); - else $Daux->generate_static(); -?> \ No newline at end of file + else $Daux->generate_static(); +?> diff --git a/index.php b/index.php index 9ace58f..bd3769f 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@ initialize(); $page = $Daux->handle_request($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); $page->display(); -?> \ No newline at end of file +?> diff --git a/libs/daux.php b/libs/Daux.php similarity index 98% rename from libs/daux.php rename to libs/Daux.php index 78393eb..343cd87 100644 --- a/libs/daux.php +++ b/libs/Daux.php @@ -1,10 +1,4 @@ -', $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); diff --git a/libs/daux_helper.php b/libs/DauxHelper.php similarity index 81% rename from libs/daux_helper.php rename to libs/DauxHelper.php index ece6258..3c7bee3 100644 --- a/libs/daux_helper.php +++ b/libs/DauxHelper.php @@ -1,5 +1,4 @@ -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(); @@ -299,65 +298,3 @@ EOT; } } - - 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; - } - } - - -?> diff --git a/libs/daux_directory.php b/libs/Entry.php similarity index 96% rename from libs/daux_directory.php rename to libs/Entry.php index b39a2ef..c31d0b7 100644 --- a/libs/daux_directory.php +++ b/libs/Entry.php @@ -1,6 +1,5 @@ -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; } } @@ -125,4 +124,3 @@ } } } -?> \ No newline at end of file diff --git a/libs/ErrorPage.php b/libs/ErrorPage.php new file mode 100644 index 0000000..4a15388 --- /dev/null +++ b/libs/ErrorPage.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/libs/MarkdownPage.php b/libs/MarkdownPage.php new file mode 100644 index 0000000..bf3395d --- /dev/null +++ b/libs/MarkdownPage.php @@ -0,0 +1,111 @@ +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); + } +} diff --git a/libs/Page.php b/libs/Page.php new file mode 100644 index 0000000..d17b952 --- /dev/null +++ b/libs/Page.php @@ -0,0 +1,7 @@ +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; + } +} diff --git a/libs/daux_page.php b/libs/daux_page.php deleted file mode 100644 index 2b9dfbc..0000000 --- a/libs/daux_page.php +++ /dev/null @@ -1,200 +0,0 @@ -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; - } - } - - 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); - } - } - - 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); - } - } - - ?> \ No newline at end of file diff --git a/templates/default/default.tpl b/templates/default/default.tpl index 147e8b3..f5c70f8 100644 --- a/templates/default/default.tpl +++ b/templates/default/default.tpl @@ -13,7 +13,7 @@ $nav = ''; foreach ($tree->value as $node) { $url = $node->uri; - if ($node->type === \TodayMade\Daux\Directory_Entry::FILE_TYPE) { + if ($node->type === \TodayMade\Daux\Entry::FILE_TYPE) { if ($node->value === 'index') continue; $nav .= ' \ No newline at end of file +?> diff --git a/templates/default/error.tpl b/templates/default/error.tpl index d05a0d0..0b31e51 100644 --- a/templates/default/error.tpl +++ b/templates/default/error.tpl @@ -12,7 +12,7 @@ private function build_navigation($tree, $path, $base_page) { $nav = ''; foreach ($tree->value as $url => $node) { - if ($node->type === \TodayMade\Daux\Directory_Entry::FILE_TYPE) { + if ($node->type === \TodayMade\Daux\Entry::FILE_TYPE) { if ($node->value === 'index') continue; $link = ($path === '') ? $url : $path . '/' . $url; $nav .= '
  • ' . $node->title . '
  • '; @@ -146,4 +146,4 @@ return $return; } } -?> \ No newline at end of file +?> diff --git a/vendor/composer/ClassLoader.php b/vendor/composer/ClassLoader.php index 47ae2ee..5e1469e 100644 --- a/vendor/composer/ClassLoader.php +++ b/vendor/composer/ClassLoader.php @@ -54,9 +54,15 @@ class ClassLoader private $useIncludePath = false; private $classMap = array(); + private $classMapAuthoritative = false; + public function getPrefixes() { - return call_user_func_array('array_merge', $this->prefixesPsr0); + if (!empty($this->prefixesPsr0)) { + return call_user_func_array('array_merge', $this->prefixesPsr0); + } + + return array(); } public function getPrefixesPsr4() @@ -143,6 +149,8 @@ class ClassLoader * @param string $prefix The prefix/namespace, with trailing '\\' * @param array|string $paths The PSR-0 base directories * @param bool $prepend Whether to prepend the directories + * + * @throws \InvalidArgumentException */ public function addPsr4($prefix, $paths, $prepend = false) { @@ -202,10 +210,13 @@ class ClassLoader * Registers a set of PSR-4 directories for a given namespace, * replacing any others previously set for this namespace. * - * @param string $prefix The prefix/namespace, with trailing '\\' - * @param array|string $paths The PSR-4 base directories + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param array|string $paths The PSR-4 base directories + * + * @throws \InvalidArgumentException */ - public function setPsr4($prefix, $paths) { + public function setPsr4($prefix, $paths) + { if (!$prefix) { $this->fallbackDirsPsr4 = (array) $paths; } else { @@ -239,6 +250,27 @@ class ClassLoader return $this->useIncludePath; } + /** + * Turns off searching the prefix and fallback directories for classes + * that have not been registered with the class map. + * + * @param bool $classMapAuthoritative + */ + public function setClassMapAuthoritative($classMapAuthoritative) + { + $this->classMapAuthoritative = $classMapAuthoritative; + } + + /** + * Should class lookup fail if not found in the current class map? + * + * @return bool + */ + public function isClassMapAuthoritative() + { + return $this->classMapAuthoritative; + } + /** * Registers this instance as an autoloader. * @@ -290,9 +322,29 @@ class ClassLoader if (isset($this->classMap[$class])) { return $this->classMap[$class]; } + if ($this->classMapAuthoritative) { + return false; + } + $file = $this->findFileWithExtension($class, '.php'); + + // Search for Hack files if we are running on HHVM + if ($file === null && defined('HHVM_VERSION')) { + $file = $this->findFileWithExtension($class, '.hh'); + } + + if ($file === null) { + // Remember that this class does not exist. + return $this->classMap[$class] = false; + } + + return $file; + } + + private function findFileWithExtension($class, $ext) + { // PSR-4 lookup - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php'; + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; $first = $class[0]; if (isset($this->prefixLengthsPsr4[$first])) { @@ -321,7 +373,7 @@ class ClassLoader . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); } else { // PEAR-like class name - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php'; + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; } if (isset($this->prefixesPsr0[$first])) { @@ -347,9 +399,6 @@ class ClassLoader if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { return $file; } - - // Remember that this class does not exist. - return $this->classMap[$class] = false; } } diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index b265c64..0b3843c 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -6,4 +6,5 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( + 'Todaymade\\Daux\\' => array($baseDir . '/libs'), ); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index a6e7514..b88bd72 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -23,9 +23,6 @@ class ComposerAutoloaderInit73a81d925b42f1d439aed1c696b3e22d self::$loader = $loader = new \Composer\Autoload\ClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInit73a81d925b42f1d439aed1c696b3e22d', 'loadClassLoader')); - $vendorDir = dirname(__DIR__); - $baseDir = dirname($vendorDir); - $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path);