Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Cotton committed Apr 8, 2012
0 parents commit ff05127
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 0 deletions.
4 changes: 4 additions & 0 deletions classes/cms.php
@@ -0,0 +1,4 @@
<?php

class Cms Extends Rpa_Cms
{}
4 changes: 4 additions & 0 deletions classes/cms/exception/notfound.php
@@ -0,0 +1,4 @@
<?php

class Cms_Exception_Notfound extends Rpa_Cms_Exception_Notfound
{}
4 changes: 4 additions & 0 deletions classes/cms/exception/unknownlocale.php
@@ -0,0 +1,4 @@
<?php

class Cms_Exception_Unknownlocale extends Rpa_Cms_Exception_Unknownlocale
{}
11 changes: 11 additions & 0 deletions classes/controller/cms.php
@@ -0,0 +1,11 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Controller_Static - controller actions for serving up static views, used when
* a view doesn't require any 'backend' processing
*
* @author Jon Cotton <jon@rpacode.co.uk>
* @copyright (c) 2011 RPA Code
*/
class Controller_Cms extends Controller_Rpa_Cms
{
}
74 changes: 74 additions & 0 deletions classes/controller/rpa/cms.php
@@ -0,0 +1,74 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Controller_Static - controller actions for serving up static views, used when
* a view doesn't require any 'backend' processing
*
* @author Jon Cotton <jon@rpacode.co.uk>
* @copyright (c) 2011 RPA Code
*/
class Controller_Rpa_Cms extends Controller
{
/**
* @static STATIC_VIEW_PATH - the directory within the application's views
* directory where the static views are located
*/
const STATIC_VIEW_PATH = 'static';

const DEFAULT_PAGE_TITLE = 'Welcome';
const TITLE_PREFIX = '';

private $page_titles_map = array(
//'' => 'Home',
//'path/to/page' => 'Page Title'
);

/**
* action_index - The default action, attempts to load the view at the path
* specified by the view parameter in the URL
*
* @throws HTTP_Exception_404
*/
public function action_index()
{
// get the parameters from the request
$view_path = $this->request->param('view', NULL);

// check if the view exists in the filesystem
try
{
$content = Cms::find_content_for_uri($view_path);
}
catch(Cms_Exception_Notfound $e)
{
throw new HTTP_Exception_404;
}

print_r($content);
exit;
}

public function action_set_locale()
{
$locale = $this->request->param('locale');

Cookie::set('locale', $locale);

$this->request->redirect('/');
}

private function get_page_title()
{
$title = Arr::get($this->page_titles_map, $this->request->uri(), FALSE);

if($title)
{
$title = self::TITLE_PREFIX.$title;
}
else
{
$title = self::DEFAULT_PAGE_TITLE;
}

return $title;
}
}
137 changes: 137 additions & 0 deletions classes/rpa/cms.php
@@ -0,0 +1,137 @@
<?php

class Rpa_Cms
{
public static $content_path = 'content';

public static $locale = 'en-us';

/**
* @var array Language paths that are used to find content
*/
protected static $_locale_paths = array();

public static function find_content_for_uri($uri, $locale = NULL)
{
// TODO: move this to init
Cms::$content_path = APPPATH.Cms::$content_path;

if($locale === NULL)
{
// no language has been supplied so get the current locale
$locale = Cms::$locale;
}

// set the key to be used to add/retrieve this content to/from the cache
$content_cache_key = 'rpa.cms.'.$locale.$uri;

if(Kohana::$caching === TRUE AND Kohana::cache($content_cache_key) !== NULL)
{
// return the content from the cache
return Kohana::cache($content_cache_key);
}

// no cache available so check that we have the locale path structure
if(empty(Cms::$_locale_paths))
{
if(Kohana::$caching === TRUE AND Kohana::cache('rpa.cms.locale_paths') !== NULL)
{
// get the locale paths from cache
Cms::$_locale_paths = Kohana::cache('rpa.cms.locale_paths');
}
else
{
// build the locale paths array from the filesystem (expensive)
$locale_paths = self::find_locale_paths(Cms::$content_path);

if(Kohana::$caching === TRUE)
{
// cache the locale paths
Kohana::cache('rpa.cms.locale_paths', $locale_paths);
}

Cms::$_locale_paths = $locale_paths;
}
}

$locale_path = Arr::get(Cms::$_locale_paths, '_'.$locale);
if($locale_path === NULL)
{
// locale not known
throw new Cms_Exception_Unknownlocale($locale);
}

$content_paths = Cms::find_content_paths($locale_path, $uri);
if(count($content_paths) < 1)
{
// content not found
throw new Cms_Exception_Notfound($locale, $uri);
}

// now we have the content paths, load the content from the yml files
$yaml = Yaml::instance();
$content = array();
foreach($content_paths as $content_path)
{
$content = Arr::merge($yaml->parse_file($content_path), $content);
}

if(Kohana::$caching === TRUE)
{
// cache the content
Kohana::cache($content_cache_key, $content);
}

return $content;
}

public static function find_locale_paths($path)
{
$locale_regex = '/^_[a-z]{2}-[a-z]{2}$/';
$handle = opendir($path);

$locale_paths = array();

while(($entry = readdir($handle)) !== FALSE)
{
$entry_path = $path.DIRECTORY_SEPARATOR.$entry;
if(is_dir($entry_path) AND preg_match($locale_regex, $entry))
{
$locale_paths[$entry] = $entry_path;

// recursively check all locale dirs to see if they contain any locales themselves
$sub_locale_paths = Cms::find_locale_paths($entry_path);
$locale_paths = Arr::merge($locale_paths, $sub_locale_paths);
}
}

return $locale_paths;
}

public static function find_content_paths($path, $uri)
{
$content_paths = array();
$content_file_path = $path.DIRECTORY_SEPARATOR.$uri;
$locale = basename($path);

if(file_exists($content_file_path.'.yml'))
{
$content_paths[$locale] = $content_file_path.'.yml';
}
elseif(is_dir($content_file_path) AND file_exists($content_file_path.DIRECTORY_SEPERATOR.'index.yml'))
{
// path is a dir so serve up the index file
$content_paths[$locale] = $content_file_path.DIRECTORY_SEPERATOR.'index.yml';
}

// check the parent locale
$parent_path = dirname($path);
if($parent_path != Cms::$content_path)
{
// the parent isn't the root content dir so check for content in the parent
$content_paths[] = Cms::find_content_paths($parent_path, $uri);
}

return Arr::flatten($content_paths);
}
}
11 changes: 11 additions & 0 deletions classes/rpa/cms/exception/notfound.php
@@ -0,0 +1,11 @@
<?php

class Rpa_Cms_Exception_Notfound extends Kohana_Exception
{

public function __construct($locale, $uri)
{
parent::__construct('Content not found for :locale at path :uri', array(':locale' => $locale, ':uri' => $uri));
}

}
11 changes: 11 additions & 0 deletions classes/rpa/cms/exception/unknownlocale.php
@@ -0,0 +1,11 @@
<?php

class Rpa_Cms_Exception_Unknownlocale extends Kohana_Exception
{

public function __construct($locale)
{
parent::__construct('Locale :locale is not known', array(':locale' => $locale));
}

}
12 changes: 12 additions & 0 deletions init.php
@@ -0,0 +1,12 @@
<?php

Route::set('cms_set_locale', 'set-locale/<locale>',
array(
'locale' => '[a-z]{2}-[a-z]{2}'
)
)->defaults(
array(
'controller' => 'cms',
'action' => 'set_locale'
)
);

0 comments on commit ff05127

Please sign in to comment.