-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
3,887 additions
and
961 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;'); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) : ''; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.