Skip to content

Commit

Permalink
Move all hardcoded folder names into config vars
Browse files Browse the repository at this point in the history
Everything is now set within extensions/config.php.
  • Loading branch information
kolber committed May 26, 2012
1 parent 451907a commit dafc902
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/asset-types/page.inc.php
Expand Up @@ -62,9 +62,9 @@ static function template_name($file_path) {


static function template_file($template_name) { static function template_file($template_name) {
$template_name = preg_replace('/([^.]*\.)?([^.]*)$/', '\\2', $template_name); $template_name = preg_replace('/([^.]*\.)?([^.]*)$/', '\\2', $template_name);
$template_file = glob('./templates/'.$template_name.'.*'); $template_file = glob(Config::$templates_folder.'/'.$template_name.'.*');
# return template if one exists # return template if one exists
return isset($template_file[0]) ? $template_file[0] : './templates/default.html'; return isset($template_file[0]) ? $template_file[0] : Config::$templates_folder.'/default.html';
} }


} }
Expand Down
8 changes: 4 additions & 4 deletions app/cache.inc.php
Expand Up @@ -11,11 +11,11 @@ function __construct($file_path, $template_file) {
# generate an md5 hash from the file_path # generate an md5 hash from the file_path
$this->path_hash = $this->generate_hash($file_path); $this->path_hash = $this->generate_hash($file_path);
# generate an md5 hash from the current state of the site content # generate an md5 hash from the current state of the site content
$htaccess = file_exists('./.htaccess') ? '.htaccess:'.filemtime('./.htaccess') : ''; $htaccess = file_exists($root_folder.'.htaccess') ? '.htaccess:'.filemtime($root_folder.'.htaccess') : '';
$file_cache = serialize(Helpers::file_cache()); $file_cache = serialize(Helpers::file_cache());
$content_hash = $this->generate_hash($htaccess.$file_cache); $content_hash = $this->generate_hash($htaccess.$file_cache);
# combine the two hashes to create a cachefile name # combine the two hashes to create a cachefile name
$this->cachefile = './app/_cache/pages/'.$this->cache_prefix.$this->path_hash.'-'.$content_hash; $this->cachefile = Config::$cache_folder.'/pages/'.$this->cache_prefix.$this->path_hash.'-'.$content_hash;
# store the hash # store the hash
$this->hash = $this->cache_prefix.$this->path_hash.'-'.$content_hash; $this->hash = $this->cache_prefix.$this->path_hash.'-'.$content_hash;
} }
Expand All @@ -32,7 +32,7 @@ function render() {


function delete_old_caches() { function delete_old_caches() {
# collect a list of all cache files matching the same file_path hash and delete them # collect a list of all cache files matching the same file_path hash and delete them
$old_caches = glob('./app/_cache/pages/'.$this->cache_prefix.$this->path_hash.'-*'); $old_caches = glob(Config::$cache_folder.'/pages/'.$this->cache_prefix.$this->path_hash.'-*');
foreach($old_caches as $file) unlink($file); foreach($old_caches as $file) unlink($file);
} }


Expand All @@ -45,7 +45,7 @@ function create($route) {
ob_start(); ob_start();
echo $page->parse_template(); echo $page->parse_template();
# if cache folder is writable, write to it # if cache folder is writable, write to it
if(is_writable('./app/_cache/pages') && !$page->data['bypass_cache']) $this->write_cache(); if(is_writable(Config::$cache_folder.'/pages') && !$page->data['bypass_cache']) $this->write_cache();
# end buffer # end buffer
ob_end_flush(); ob_end_flush();
return ''; return '';
Expand Down
11 changes: 6 additions & 5 deletions app/helpers.inc.php
Expand Up @@ -31,7 +31,7 @@ static function url_to_file_path($url) {
# if the url is empty, we're looking for the index page # if the url is empty, we're looking for the index page
$url = empty($url) ? 'index': $url; $url = empty($url) ? 'index': $url;


$file_path = './content'; $file_path = Config::$content_folder;
# Split the url and recursively unclean the parts into folder names # Split the url and recursively unclean the parts into folder names
$url_parts = explode('/', $url); $url_parts = explode('/', $url);
foreach($url_parts as $u) { foreach($url_parts as $u) {
Expand All @@ -53,9 +53,9 @@ static function has_children($dir) {
static function file_cache($dir = false) { static function file_cache($dir = false) {
if(!self::$file_cache) { if(!self::$file_cache) {
# build file cache # build file cache
self::build_file_cache('./app'); self::build_file_cache(Config::$app_folder);
self::build_file_cache('./content'); self::build_file_cache(Config::$content_folder);
self::build_file_cache('./templates'); self::build_file_cache(Config::$templates_folder);
} }
if($dir && !isset(self::$file_cache[$dir])) return array(); if($dir && !isset(self::$file_cache[$dir])) return array();
return $dir ? self::$file_cache[$dir] : self::$file_cache; return $dir ? self::$file_cache[$dir] : self::$file_cache;
Expand Down Expand Up @@ -133,7 +133,8 @@ static function last_modified($dir) {
return $last_modified; return $last_modified;
} }


static function site_last_modified($dir = './content') { static function site_last_modified($dir = false) {
if (!$dir) $dir = Config::$content_folder;
$last_updated = 0; $last_updated = 0;
foreach(Helpers::list_files($dir, '/.*/', false) as $file) { foreach(Helpers::list_files($dir, '/.*/', false) as $file) {
if(filemtime($file) > $last_updated) $last_updated = filemtime($file); if(filemtime($file) > $last_updated) $last_updated = filemtime($file);
Expand Down
6 changes: 3 additions & 3 deletions app/page-data.inc.php
Expand Up @@ -28,7 +28,7 @@ static function get_parent($file_path, $url) {
array_pop($split_path); array_pop($split_path);
$parent_path = array(implode('/', $split_path)); $parent_path = array(implode('/', $split_path));


return $parent_path[0] == './content' ? array() : $parent_path; return $parent_path[0] == Config::$content_folder ? array() : $parent_path;
} }


static function get_parents($file_path, $url) { static function get_parents($file_path, $url) {
Expand Down Expand Up @@ -143,7 +143,7 @@ static function create_vars($page) {


static function create_collections($page) { static function create_collections($page) {
# page.root # page.root
$page->root = Helpers::list_files('./content', '/^\d+?\./', true); $page->root = Helpers::list_files(Config::$content_folder, '/^\d+?\./', true);
# page.query # page.query
$page->query = $_GET; $page->query = $_GET;
# page.parent # page.parent
Expand All @@ -152,7 +152,7 @@ static function create_collections($page) {
# page.parents # page.parents
$page->parents = self::get_parents($page->file_path, $page->url_path); $page->parents = self::get_parents($page->file_path, $page->url_path);
# page.siblings # page.siblings
$parent_path = !empty($parent_path[0]) ? $parent_path[0] : './content'; $parent_path = !empty($parent_path[0]) ? $parent_path[0] : Config::$content_folder;
$split_url = explode("/", $page->url_path); $split_url = explode("/", $page->url_path);
$page->siblings = Helpers::list_files($parent_path, '/^\d+?\.(?!'.$split_url[(count($split_url) - 1)].')/', true); $page->siblings = Helpers::list_files($parent_path, '/^\d+?\.(?!'.$split_url[(count($split_url) - 1)].')/', true);
# page.siblings_and_self # page.siblings_and_self
Expand Down
5 changes: 3 additions & 2 deletions app/parsers/slir/slirconfig.class.php
Expand Up @@ -26,6 +26,7 @@
/* $Id: slirconfig-sample.class.php 123 2010-12-21 18:58:03Z joe.lencioni $ */ /* $Id: slirconfig-sample.class.php 123 2010-12-21 18:58:03Z joe.lencioni $ */


require_once 'slirconfigdefaults.class.php'; require_once 'slirconfigdefaults.class.php';
require_once '../../../extensions/config.php';


/** /**
* SLIR Config Class * SLIR Config Class
Expand All @@ -44,11 +45,11 @@ class SLIRConfig extends SLIRConfigDefaults


public static function init() public static function init()
{ {
self::$cacheDir = '../../_cache/images'; self::$cacheDir = '../../../'.Config::$cache_folder.'/images';
self::$documentRoot = '../../..'; self::$documentRoot = '../../..';
// This must be the last line of this function // This must be the last line of this function
parent::init(); parent::init();
} }
} }


SLIRConfig::init(); SLIRConfig::init();
4 changes: 2 additions & 2 deletions app/parsers/template-parser.inc.php
Expand Up @@ -14,8 +14,8 @@ static function parse($data, $template) {
$template = self::find_template($template); $template = self::find_template($template);


Twig_Autoloader::register(); Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates'); $loader = new Twig_Loader_Filesystem(Config::$templates_folder);
$cache = is_writable('app/_cache/templates') ? 'app/_cache/templates' : false; $cache = is_writable(Config::$cache_folder.'/templates') ? Config::$cache_folder.'/templates' : false;
$twig = new Twig_Environment($loader, array( $twig = new Twig_Environment($loader, array(
'cache' => $cache, 'cache' => $cache,
'auto_reload' => true, 'auto_reload' => true,
Expand Down
4 changes: 2 additions & 2 deletions app/stacey.inc.php
Expand Up @@ -136,8 +136,8 @@ function __construct($get) {
if($e->getMessage() == "404") { if($e->getMessage() == "404") {
# return 404 headers # return 404 headers
header('HTTP/1.0 404 Not Found'); header('HTTP/1.0 404 Not Found');
if(file_exists('./content/404')) { if(file_exists(Config::$content_folder.'/404')) {
$this->create_page('./content/404', '404'); $this->create_page(Config::$content_folder.'/404', '404');
} }
else if(file_exists('./public/404.html')) { else if(file_exists('./public/404.html')) {
echo file_get_contents('./public/404.html'); echo file_get_contents('./public/404.html');
Expand Down
14 changes: 14 additions & 0 deletions extensions/config.php
@@ -0,0 +1,14 @@
<?php

class Config {

public static $root_folder = './';
public static $app_folder = './app';
public static $content_folder = './content';
public static $templates_folder = './templates';
public static $cache_folder = './app/_cache';
public static $extensions_folder = './extensions';

}

?>
2 changes: 2 additions & 0 deletions index.php
Expand Up @@ -7,6 +7,8 @@


} else { } else {


# require config
require_once './extensions/config.php';
# require helpers class so we can use rglob # require helpers class so we can use rglob
require_once './app/helpers.inc.php'; require_once './app/helpers.inc.php';
# require the yaml parser # require the yaml parser
Expand Down

0 comments on commit dafc902

Please sign in to comment.