Merge pull request #234 from kushipl/master

Apply PSR1/PSR2 coding standards.
This commit is contained in:
Tyler Heshka 2015-06-12 13:26:16 -04:00
commit 6e50ac5cc8
3 changed files with 433 additions and 398 deletions

View File

@ -1,13 +1,13 @@
<?php
define('ROOT_DIR', realpath(dirname(__FILE__)) .'/');
define('CONTENT_DIR', ROOT_DIR .'content-sample/');
define('ROOT_DIR', realpath(dirname(__FILE__)) . '/');
define('CONTENT_DIR', ROOT_DIR . 'content-sample/');
define('CONTENT_EXT', '.md');
define('LIB_DIR', ROOT_DIR .'lib/');
define('PLUGINS_DIR', ROOT_DIR .'plugins/');
define('THEMES_DIR', ROOT_DIR .'themes/');
define('CACHE_DIR', LIB_DIR .'cache/');
define('LIB_DIR', ROOT_DIR . 'lib/');
define('PLUGINS_DIR', ROOT_DIR . 'plugins/');
define('THEMES_DIR', ROOT_DIR . 'themes/');
define('CACHE_DIR', LIB_DIR . 'cache/');
require_once(ROOT_DIR .'vendor/autoload.php');
require_once(LIB_DIR .'pico.php');
require_once(ROOT_DIR . 'vendor/autoload.php');
require_once(LIB_DIR . 'pico.php');
$pico = new Pico();

View File

@ -8,7 +8,8 @@
* @license http://opensource.org/licenses/MIT
* @version 0.8
*/
class Pico {
class Pico
{
private $config;
private $plugins;
@ -33,25 +34,34 @@ class Pico {
$script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';
// Get our url path and trim the / of the left and the right
if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
if ($request_url != $script_url) {
$url = trim(preg_replace('/' . str_replace('/', '\/', str_replace('index.php', '', $script_url)) . '/', '',
$request_url, 1), '/');
}
$url = preg_replace('/\?.*/', '', $url); // Strip query string
$this->run_hooks('request_url', array(&$url));
// Get the file path
if($url) $file = $settings['content_dir'] . $url;
else $file = $settings['content_dir'] .'index';
if ($url) {
$file = $settings['content_dir'] . $url;
} else {
$file = $settings['content_dir'] . 'index';
}
// Load the file
if(is_dir($file)) $file = $settings['content_dir'] . $url .'/index'. CONTENT_EXT;
else $file .= CONTENT_EXT;
if (is_dir($file)) {
$file = $settings['content_dir'] . $url . '/index' . CONTENT_EXT;
} else {
$file .= CONTENT_EXT;
}
$this->run_hooks('before_load_content', array(&$file));
if(file_exists($file)){
if (file_exists($file)) {
$content = file_get_contents($file);
} else {
$this->run_hooks('before_404_load_content', array(&$file));
$content = file_get_contents($settings['content_dir'] .'404'. CONTENT_EXT);
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
$content = file_get_contents($settings['content_dir'] . '404' . CONTENT_EXT);
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
$this->run_hooks('after_404_load_content', array(&$file, &$content));
}
$this->run_hooks('after_load_content', array(&$file, &$content));
@ -65,12 +75,13 @@ class Pico {
$this->run_hooks('content_parsed', array(&$content)); // Depreciated @ v0.8
// Get all the pages
$pages = $this->get_pages($settings['base_url'], $settings['pages_order_by'], $settings['pages_order'], $settings['excerpt_length']);
$pages = $this->get_pages($settings['base_url'], $settings['pages_order_by'], $settings['pages_order'],
$settings['excerpt_length']);
$prev_page = array();
$current_page = array();
$next_page = array();
while($current_page = current($pages)){
if((isset($meta['title'])) && ($meta['title'] == $current_page['title'])){
while ($current_page = current($pages)) {
if ((isset($meta['title'])) && ($meta['title'] == $current_page['title'])) {
break;
}
next($pages);
@ -91,7 +102,7 @@ class Pico {
'base_dir' => rtrim(ROOT_DIR, '/'),
'base_url' => $settings['base_url'],
'theme_dir' => THEMES_DIR . $settings['theme'],
'theme_url' => $settings['base_url'] .'/'. basename(THEMES_DIR) .'/'. $settings['theme'],
'theme_url' => $settings['base_url'] . '/' . basename(THEMES_DIR) . '/' . $settings['theme'],
'site_title' => $settings['site_title'],
'meta' => $meta,
'content' => $content,
@ -104,7 +115,7 @@ class Pico {
$template = (isset($meta['template']) && $meta['template']) ? $meta['template'] : 'index';
$this->run_hooks('before_render', array(&$twig_vars, &$twig, &$template));
$output = $twig->render($template .'.html', $twig_vars);
$output = $twig->render($template . '.html', $twig_vars);
$this->run_hooks('after_render', array(&$output));
echo $output;
}
@ -116,11 +127,11 @@ class Pico {
{
$this->plugins = array();
$plugins = $this->get_files(PLUGINS_DIR, '.php');
if(!empty($plugins)){
foreach($plugins as $plugin){
if (!empty($plugins)) {
foreach ($plugins as $plugin) {
include_once($plugin);
$plugin_name = preg_replace("/\\.[^.\\s]{3}$/", '', basename($plugin));
if(class_exists($plugin_name)){
if (class_exists($plugin_name)) {
$obj = new $plugin_name;
$this->plugins[] = $obj;
}
@ -165,15 +176,17 @@ class Pico {
// Add support for custom headers by hooking into the headers array
$this->run_hooks('before_read_file_meta', array(&$headers));
foreach ($headers as $field => $regex){
if (preg_match('/^[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $content, $match) && $match[1]){
$headers[ $field ] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $match[1]));
foreach ($headers as $field => $regex) {
if (preg_match('/^[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $content, $match) && $match[1]) {
$headers[$field] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $match[1]));
} else {
$headers[ $field ] = '';
$headers[$field] = '';
}
}
if(isset($headers['date'])) $headers['date_formatted'] = utf8_encode(strftime($config['date_format'], strtotime($headers['date'])));
if (isset($headers['date'])) {
$headers['date_formatted'] = utf8_encode(strftime($config['date_format'], strtotime($headers['date'])));
}
return $headers;
}
@ -186,7 +199,7 @@ class Pico {
protected function get_config()
{
$this->config = @include_once(ROOT_DIR .'config.php');
$this->config = @include_once(ROOT_DIR . 'config.php');
$defaults = array(
'site_title' => 'Pico',
@ -200,8 +213,11 @@ class Pico {
'content_dir' => 'content-sample/',
);
if(is_array($this->config)) $this->config = array_merge($defaults, $this->config);
else $this->config = $defaults;
if (is_array($this->config)) {
$this->config = array_merge($defaults, $this->config);
} else {
$this->config = $defaults;
}
return $this->config;
}
@ -221,15 +237,15 @@ class Pico {
$pages = $this->get_files($config['content_dir'], CONTENT_EXT);
$sorted_pages = array();
$date_id = 0;
foreach($pages as $key=>$page){
foreach ($pages as $key => $page) {
// Skip 404
if(basename($page) == '404'. CONTENT_EXT){
if (basename($page) == '404' . CONTENT_EXT) {
unset($pages[$key]);
continue;
}
// Ignore Emacs (and Nano) temp files
if (in_array(substr($page, -1), array('~','#'))) {
if (in_array(substr($page, -1), array('~', '#'))) {
unset($pages[$key]);
continue;
}
@ -237,15 +253,16 @@ class Pico {
$page_content = file_get_contents($page);
$page_meta = $this->read_file_meta($page_content);
$page_content = $this->parse_content($page_content);
$url = str_replace($config['content_dir'], $base_url .'/', $page);
$url = str_replace('index'. CONTENT_EXT, '', $url);
$url = str_replace($config['content_dir'], $base_url . '/', $page);
$url = str_replace('index' . CONTENT_EXT, '', $url);
$url = str_replace(CONTENT_EXT, '', $url);
$data = array(
'title' => isset($page_meta['title']) ? $page_meta['title'] : '',
'url' => $url,
'author' => isset($page_meta['author']) ? $page_meta['author'] : '',
'date' => isset($page_meta['date']) ? $page_meta['date'] : '',
'date_formatted' => isset($page_meta['date']) ? utf8_encode(strftime($config['date_format'], strtotime($page_meta['date']))) : '',
'date_formatted' => isset($page_meta['date']) ? utf8_encode(strftime($config['date_format'],
strtotime($page_meta['date']))) : '',
'content' => $page_content,
'excerpt' => $this->limit_words(strip_tags($page_content), $excerpt_length),
//this addition allows the 'description' meta to be picked up in content areas... specifically to replace 'excerpt'
@ -256,15 +273,19 @@ class Pico {
// Extend the data provided with each page by hooking into the data array
$this->run_hooks('get_page_data', array(&$data, $page_meta));
if($order_by == 'date' && isset($page_meta['date'])){
$sorted_pages[$page_meta['date'].$date_id] = $data;
if ($order_by == 'date' && isset($page_meta['date'])) {
$sorted_pages[$page_meta['date'] . $date_id] = $data;
$date_id++;
} else {
$sorted_pages[$page] = $data;
}
else $sorted_pages[$page] = $data;
}
if($order == 'desc') krsort($sorted_pages);
else ksort($sorted_pages);
if ($order == 'desc') {
krsort($sorted_pages);
} else {
ksort($sorted_pages);
}
return $sorted_pages;
}
@ -277,9 +298,9 @@ class Pico {
*/
protected function run_hooks($hook_id, $args = array())
{
if(!empty($this->plugins)){
foreach($this->plugins as $plugin){
if(is_callable(array($plugin, $hook_id))){
if (!empty($this->plugins)) {
foreach ($this->plugins as $plugin) {
if (is_callable(array($plugin, $hook_id))) {
call_user_func_array(array($plugin, $hook_id), $args);
}
}
@ -295,14 +316,20 @@ class Pico {
{
$config = $this->config;
if(isset($config['base_url']) && $config['base_url']) return $config['base_url'];
if (isset($config['base_url']) && $config['base_url']) {
return $config['base_url'];
}
$url = '';
$request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
$script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';
if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
if ($request_url != $script_url) {
$url = trim(preg_replace('/' . str_replace('/', '\/', str_replace('index.php', '', $script_url)) . '/', '',
$request_url, 1), '/');
}
$protocol = $this->get_protocol();
return rtrim(str_replace($url, '', $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']), '/');
}
@ -314,9 +341,10 @@ class Pico {
protected function get_protocol()
{
$protocol = 'http';
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' && $_SERVER['HTTPS'] != ''){
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' && $_SERVER['HTTPS'] != '') {
$protocol = 'https';
}
return $protocol;
}
@ -330,22 +358,25 @@ class Pico {
protected function get_files($directory, $ext = '')
{
$array_items = array();
if($handle = opendir($directory)){
while(false !== ($file = readdir($handle))){
if(in_array(substr($file, -1), array('~', '#'))){
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if (in_array(substr($file, -1), array('~', '#'))) {
continue;
}
if(preg_match("/^(^\.)/", $file) === 0){
if(is_dir($directory. "/" . $file)){
$array_items = array_merge($array_items, $this->get_files($directory. "/" . $file, $ext));
if (preg_match("/^(^\.)/", $file) === 0) {
if (is_dir($directory . "/" . $file)) {
$array_items = array_merge($array_items, $this->get_files($directory . "/" . $file, $ext));
} else {
$file = $directory . "/" . $file;
if(!$ext || strstr($file, $ext)) $array_items[] = preg_replace("/\/\//si", "/", $file);
if (!$ext || strstr($file, $ext)) {
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
}
closedir($handle);
}
return $array_items;
}
@ -358,9 +389,12 @@ class Pico {
*/
protected function limit_words($string, $word_limit)
{
$words = explode(' ',$string);
$words = explode(' ', $string);
$excerpt = trim(implode(' ', array_splice($words, 0, $word_limit)));
if(count($words) > $word_limit) $excerpt .= '&hellip;';
if (count($words) > $word_limit) {
$excerpt .= '&hellip;';
}
return $excerpt;
}

View File

@ -7,7 +7,8 @@
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT
*/
class Pico_Plugin {
class Pico_Plugin
{
public function plugins_loaded()
{