Skip to content

Commit

Permalink
Updated app to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kolber committed Jan 7, 2010
1 parent 26804f9 commit 38eeca0
Show file tree
Hide file tree
Showing 19 changed files with 3,887 additions and 961 deletions.
27 changes: 18 additions & 9 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ RewriteEngine on

ErrorDocument 404 /404.html

# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !app/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /* to the app
RewriteCond %{REQUEST_URI} app/$
RewriteRule ^app - [L]
RewriteRule ^(.*)/$ app/?$1 [L]
RewriteRule ^$ app/ [L]

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_URI} !(app|public|content)/(.*)$
RewriteRule ^(.+) public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]
File renamed without changes.
61 changes: 61 additions & 0 deletions app/asset-types/asset-factory.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

Class AssetFactory {

static $store;
static $asset_subclasses = array();

static function &create($file_path) {

#
# a little bit of magic here to find any classes which extend 'Asset'
#
self::get_asset_subclasses();

# if the file path isn't passed through as a string, return an empty data array
$data = array();
if(!is_string($file_path)) return $data;

# split by file extension
preg_match('/\.([\w\d]+?)$/', $file_path, $split_path);

if(isset($split_path[1]) && !is_dir($file_path)) {
# set the default asset type
$asset = 'Asset';
# loop through our asset_subclasses to see if this filetype should be handled in a special way
foreach(self::$asset_subclasses as $asset_type => $identifiers) {
# if a match is found, set $asset to be the name of the matching class
if(in_array(strtolower($split_path[1]), $identifiers)) $asset = $asset_type;
}

# create a new asset and return its data
$asset = new $asset($file_path);
return $asset->data;

} else {
# new page
$page = new Page(Helpers::file_path_to_url($file_path));
return $page->data;
}
}

static function &get($key) {
# if object doesn't exist, create it
if(!isset(self::$store[$key])) self::$store[$key] =& self::create($key);
return self::$store[$key];
}

static function get_asset_subclasses() {
# if asset_subclasses hasn't been filled yet
if(empty(self::$asset_subclasses)) {
# loop through each declared class
foreach(get_declared_classes() as $class) {
# if the class extends 'Asset', then push it into our asset_subclasses hash
if(strtolower(get_parent_class($class)) == 'asset') self::$asset_subclasses[$class] = eval('return '.$class.'::$identifiers;');
}
}
}

}

?>
35 changes: 35 additions & 0 deletions app/asset-types/asset.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

Class Asset {

var $data;
var $link_path;
var $file_name;
static $identifiers;

function __construct($file_path) {
# create and store data required for this asset
$this->set_default_data($file_path);
}

function construct_link_path($file_path) {
return preg_replace('/^\.\//', Helpers::relative_root_path(), $file_path);
}

function set_default_data($file_path) {
# store link path
$this->link_path = $this->construct_link_path($file_path);

# extract filename from path
$split_path = explode('/', $file_path);
$this->file_name = array_pop($split_path);

# set @url & @name variables
$this->data['@url'] = $this->link_path;
$this->data['@file_name'] = $this->file_name;
$this->data['@name'] = ucfirst(preg_replace(array('/[-_]/', '/\.[\w\d]+?$/', '/^\d+?\./'), array(' ', '', ''), $this->file_name));
}

}

?>
18 changes: 18 additions & 0 deletions app/asset-types/html.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

Class Html extends Asset {

static $identifiers = array('html', 'htm');

function __construct($file_path) {
# create and store data required for this asset
parent::__construct($file_path);
# create and store additional data required for this asset
$this->set_extended_data($file_path);
}
function set_extended_data($file_path) {
$this->data['@content'] = is_readable($file_path) ? file_get_contents($file_path) : '';
}
}

?>
35 changes: 35 additions & 0 deletions app/asset-types/image.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

Class Image extends Asset {

static $identifiers = array('jpg', 'jpeg', 'gif', 'png');

function __construct($file_path) {
# create and store data required for this asset
parent::__construct($file_path);
# create and store additional data required for this asset
$this->set_extended_data($file_path);
}

function set_extended_data($file_path) {

$small_version_path = preg_replace('/(\.[\w\d]+?)$/', '_sml$1', $this->link_path);
$large_version_path = preg_replace('/(\.[\w\d]+?)$/', '_lge$1', $this->link_path);

# if a matching _sml version exists, set @small
$small_relative_path = preg_replace('/(\.\.\/)+/', './', $small_version_path);
if(file_exists($small_relative_path) && !is_dir($small_relative_path)) {
$this->data['@small'] = $small_version_path;
}

# if a matching _lge version exists, set @large
$large_relative_path = preg_replace('/(\.\.\/)+/', './', $large_version_path);
if(file_exists($large_relative_path) && !is_dir($large_relative_path)) {
$this->data['@large'] = $large_version_path;
}

}

}

?>
56 changes: 56 additions & 0 deletions app/asset-types/page.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

Class Page {

var $url_path;
var $file_path;
var $template_name;
var $template_file;
var $data;
var $all_pages;

function __construct($url) {

# store url and converted file path
$this->url_path = $url;
$this->file_path = Helpers::url_to_file_path($this->url_path);

$this->template_name = $this->template_name();
$this->template_file = $this->template_file();

# create/set all content variables
PageData::create($this);
# sort data array by key length
#
# this ensures that something like '@title' doesn't turn '@page_title'
# into '@page_Contents of @title variable' in the final rendered template
#
uksort($this->data, array('Helpers', 'sort_by_length'));

}

function parse_template() {
return TemplateParser::parse($this->data, file_get_contents($this->template_file));
}

# magic variable assignment
function __set($name, $value) {
$prefix = is_array($value) ? '$' : '@';
$this->data[$prefix.strtolower($name)] = $value;
}

function template_name() {
$txts = array_keys(Helpers::list_files($this->file_path, '/\.txt$/'));
# return first matched .txt file
return (!empty($txts)) ? preg_replace('/\.txt$/', '', $txts[0]) : false;
}

function template_file() {
$template_file = glob('./templates/'.$this->template_name.'.{html,json,atom,rss,rdf,xml,txt}', GLOB_BRACE);
# return template if one exists
return isset($template_file[0]) ? $template_file[0] : false;
}

}

?>
23 changes: 23 additions & 0 deletions app/asset-types/video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

Class Video extends Asset {

static $identifiers = array('mov', 'mp4', 'm4v', 'swf');

function __construct($file_path) {
# create and store data required for this asset
parent::__construct($file_path);
# create and store additional data required for this asset
$this->set_extended_data($file_path);
}

function set_extended_data($file_path) {
if(preg_match('/(\d+?)x(\d+?)\./', $this->file_name, $matches)) $dimensions = array('width' => $matches[1], 'height' => $matches[2]);
else $dimensions = array('width' => '', 'height' => '');
$this->data['@width'] = $dimensions['width'];
$this->data['@height'] = $dimensions['height'];
}

}

?>
77 changes: 77 additions & 0 deletions app/cache.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

Class Cache {

var $page;
var $cachefile;
var $hash;

function __construct($page) {
# store reference to current page
$this->page = $page;
# turn a base64 of the full path to the page's content file into the name of the cache file
$content_file_path = $page->file_path.'/'.$page->template_name.'.txt';
$this->cachefile = './app/_cache/'.base64_encode($content_file_path);
//collect an md5 of all files
$this->hash = $this->create_hash();
}

function render() {
return file_get_contents($this->cachefile)."\n".'<!-- Cached. -->';
}

function create($page) {
# start output buffer
ob_start();
echo $page->parse_template();
# if cache folder is writable, write to it
if(is_writable('./app/_cache')) $this->write_cache();
else echo "\n".'<!-- Stacey('.Stacey::$version.'). -->';
# end buffer
ob_end_flush();
return '';
}

function expired() {
# if cachefile doesn't exist, we need to create one
if(!file_exists($this->cachefile)) return true;
# compare new m5d to existing cached md5
elseif($this->hash !== $this->get_current_hash()) return true;
else return false;
}

function get_current_hash() {
preg_match('/Stacey.*: (.+?)\s/', file_get_contents($this->cachefile), $matches);
return $matches[1];
}

function write_cache() {
echo "\n".'<!-- Stacey('.Stacey::$version.'): '.$this->hash.' -->';
$fp = fopen($this->cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
}

function create_hash() {
# create a collection of every file inside the content folder
$content = $this->collate_files('./content');
# create a collection of every file inside the templates folder
$templates = $this->collate_files('./templates');
# create a collection of every file inside the public folder
$public = $this->collate_files('./public');
# create an md5 of the two collections
return $this->hash = md5($content.$templates.$public);
}

function collate_files($dir) {
$files_modified = '';
foreach(Helpers::list_files($dir, '/.*/', false) as $file) {
$files_modified .= $file.':'.filemtime($file);
if(is_dir($file)) $files_modified .= $this->collate_files($file);
}
return $files_modified;
}

}

?>
Binary file removed app/cache/.DS_Store
Binary file not shown.
Loading

0 comments on commit 38eeca0

Please sign in to comment.