Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Flattened the settings array pretty much everywhere I can think of, a…
Browse files Browse the repository at this point in the history
…lso updated the tests to reflect that
  • Loading branch information
hassankhan committed Feb 17, 2014
1 parent 06b28fe commit c612b90
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 179 deletions.
38 changes: 16 additions & 22 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
<?php

$config = array(
'zepto' => array(
'environment' => 'dev',
'content_dir' => 'content',
'plugins_dir' => 'plugins',
'templates_dir' => 'templates',
'default_template' => 'base.twig',
'content_ext' => array('.md', '.markdown'),
'plugins_enabled' => true
),
'site' => array(
'site_root' => 'Site root URL goes here',
'site_title' => 'Zepto',
'date_format' => 'jS M Y',
'excerpt_length' => '50',
'nav' => array(
'class' => 'nav',
'dropdown_li_class' => 'dropdown',
'dropdown_ul_class' => 'dropdown-menu',
'dropdown_li_markup' => '<li class="%s"><a href="%s" class="dropdown-toggle" data-toggle="dropdown"> %s <b class="caret"></b></a><ul class="%s">'
)
),
'twig' => array(
'zepto.environment' => 'dev',
'zepto.content_dir' => 'content',
'zepto.plugins_dir' => 'plugins',
'zepto.templates_dir' => 'templates',
'zepto.default_template' => 'base.twig',
'zepto.content_ext' => array('.md', '.markdown'),
'zepto.plugins_enabled' => true,
'site.site_root' => 'http://localhost:8888/zepto/',
'site.site_title' => 'Zepto',
'site.date_format' => 'jS M Y',
'site.excerpt_length' => '50',
'site.nav.class' => 'nav',
'site.nav.dropdown_li_class' => 'dropdown',
'site.nav.dropdown_ul_class' => 'dropdown-menu',
'site.nav.dropdown_li_markup' => '<li class="%s"><a href="%s" class="dropdown-toggle" data-toggle="dropdown"> %s <b class="caret"></b></a><ul class="%s">',
'twig' => array(
'charset' => 'utf-8',
'cache' => 'cache',
'strict_variables' => false,
Expand Down
54 changes: 22 additions & 32 deletions library/Zepto/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Zepto;

/**
* Helper class to hold all helper-related functions
* Helper class to hold all helper-y functions
*
*
* @package Zepto
* @subpackage Helper
* @author Hassan Khan <contact@hassankhan.me>
* @link http://https://github.com/hassankhan/Zepto
* @link https://github.com/hassankhan/Zepto
* @license MIT
* @since 0.6
*/
Expand Down Expand Up @@ -44,27 +44,18 @@ public function __construct(\Pimple $app)
public static function default_config()
{
return array(
'zepto' => array(
'environment' => 'dev',
'content_dir' => 'content',
'plugins_dir' => 'plugins',
'templates_dir' => 'templates',
'default_template' => 'base.twig',
'content_ext' => array('.md', '.markdown'),
'plugins_enabled' => true
),
'site' => array(
'site_root' => 'http://localhost:8888/zepto/',
'site_title' => 'Zepto',
'date_format' => 'jS M Y',
'excerpt_length' => '50',
'nav' => array(
'class' => 'nav',
'dropdown_li_class' => 'dropdown',
'dropdown_ul_class' => 'dropdown-menu'
)
),
'twig' => array(
'zepto.environment' => 'dev',
'zepto.content_dir' => 'content',
'zepto.plugins_dir' => 'plugins',
'zepto.templates_dir' => 'templates',
'zepto.default_template' => 'base.twig',
'zepto.content_ext' => array('.md', '.markdown'),
'zepto.plugins_enabled' => false,
'site.site_root' => 'http://localhost:8888/zepto/',
'site.site_title' => 'Zepto',
'site.date_format' => 'jS M Y',
'site.excerpt_length' => '50',
'twig' => array(
'charset' => 'utf-8',
'cache' => 'cache',
'strict_variables' => false,
Expand All @@ -86,30 +77,30 @@ public static function validate_config($config)
$message = '';

while ($message === '') {
if (!is_dir($config['zepto']['content_dir'])) {
if (!is_dir($config['zepto.content_dir'])) {
$message = 'Content directory does not exist';
break;
}

if (!is_dir($config['zepto']['plugins_dir'])) {
if (!is_dir($config['zepto.plugins_dir'])) {
$message = 'Plugins directory does not exist';
break;
}

if (!is_dir($config['zepto']['templates_dir'])) {
if (!is_dir($config['zepto.templates_dir'])) {
$message = 'Templates directory does not exist';
break;
}

if (
!is_file("{$config['zepto']['templates_dir']}/{$config['zepto']['default_template']}")
!is_file("{$config['zepto.templates_dir']}/{$config['zepto.default_template']}")
) {
$message = 'No default template exists';
break;
}

if ($config['zepto']['environment'] !== 'dev') {
preg_match('#^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/+$#', $config['site']['site_root']) === 0
if ($config['zepto.environment'] !== 'dev') {
preg_match('#^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/+$#', $config['site.site_root']) === 0
? $message = 'Site root is invalid. Should be like http://www.example.com/'
: $message = '';
}
Expand Down Expand Up @@ -139,17 +130,16 @@ public function url_for($file_name)

// Create URL and return
$clean_file_name = str_replace(
array_merge(array('index'), $this->app['settings']['zepto']['content_ext']),
array_merge(array('index'), $this->app['settings']['zepto.content_ext']),
'',
$file_name
);
return trim($this->app['settings']['site']['site_root'] . $clean_file_name, '/') . '/';
return trim($this->app['settings']['site.site_root'] . $clean_file_name, '/') . '/';
}
catch (\Exception $e) {
$this->app['router']->error($e);
}
return null;

}

/**
Expand Down
47 changes: 9 additions & 38 deletions library/Zepto/Zepto.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,6 @@ class Zepto {
/**
* Zepto constructor
*
* <code>
* $config = array(
* 'zepto' => array(
* 'environment' => 'dev',
* 'content_dir' => 'content',
* 'plugins_dir' => 'plugins',
* 'templates_dir' => 'templates',
* 'default_template' => 'base.twig',
* 'content_ext' => array('.md', '.markdown'),
* 'plugins_enabled' => true
* ),
* 'site' => array(
* 'site_root' => 'Site root URL goes here',
* 'site_title' => 'Zepto',
* 'date_format' => 'jS M Y',
* 'excerpt_length' => '50',
* 'nav' => array(
* 'class' => 'nav',
* 'dropdown_li_class' => 'dropdown',
* 'dropdown_ul_class' => 'dropdown-menu'
* )
* ),
* 'twig' => array(
* 'charset' => 'utf-8',
* 'cache' => 'cache',
* 'strict_variables' => false,
* 'autoescape' => false,
* 'auto_reload' => true
* )
* );
* </code>
* @param array $settings
*/
public function __construct(array $settings = array())
Expand Down Expand Up @@ -111,7 +80,7 @@ function ($app) {
$app['content_loader'] = $app->share(
function ($app) {
return new FileLoader\MarkdownLoader(
$app['ROOT_DIR'] . $app['settings']['zepto']['content_dir'],
$app['ROOT_DIR'] . $app['settings']['zepto.content_dir'],
new \Michelf\MarkdownExtra
);
}
Expand All @@ -126,7 +95,9 @@ function ($app) {
$app['twig'] = $app->share(
function($app) {
$twig = new \Twig_Environment(
new \Twig_Loader_Filesystem($app['ROOT_DIR'] . 'templates')
new \Twig_Loader_Filesystem($app['ROOT_DIR'] . 'templates',
$app['settings']['twig']
)
);
$twig->addExtension(new Extension\Twig);
return $twig;
Expand All @@ -143,15 +114,15 @@ function($app) {
}

// Set this particular setting now
$app['plugins_enabled'] = $settings['zepto']['plugins_enabled'];
$app['plugins_enabled'] = $settings['zepto.plugins_enabled'];

// So if plugins ARE indeed enabled, initialise the plugin loader
// and load the fuckers
if ($app['plugins_enabled'] === true) {
$app['plugin_loader'] = $app->share(
function($c) use ($settings) {
return new FileLoader\PluginLoader(
$c['ROOT_DIR'] . $settings['zepto']['plugins_dir']
$c['ROOT_DIR'] . $settings['zepto.plugins_dir']
);
}
);
Expand Down Expand Up @@ -267,8 +238,8 @@ protected function setup_router()
// Set Twig options
$twig_vars = array(
'config' => $app['settings'],
'base_url' => $app['settings']['site']['site_root'],
'site_title' => $app['settings']['site']['site_title']
'base_url' => $app['settings']['site.site_root'],
'site_title' => $app['settings']['site.site_title']
);

$app['nav'] = isset($app['nav']) === TRUE ? $app['nav'] : array();
Expand All @@ -280,7 +251,7 @@ protected function setup_router()
// Get template name from file, if not set, then use default
$template_name = array_key_exists('template', $content['meta']) === true
? $content['meta']['template']
: $app['settings']['zepto']['default_template'];
: $app['settings']['zepto.default_template'];

// Render template with Twig
return $app['twig']->render($template_name, $options);
Expand Down
51 changes: 21 additions & 30 deletions tests/Zepto/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,18 @@ protected function tearDown()
public function testDefaultConfig()
{
$expected = array(
'zepto' => array(
'environment' => 'dev',
'content_dir' => 'content',
'plugins_dir' => 'plugins',
'templates_dir' => 'templates',
'default_template' => 'base.twig',
'content_ext' => array('.md', '.markdown'),
'plugins_enabled' => true
),
'site' => array(
'site_root' => 'http://localhost:8888/zepto/',
'site_title' => 'Zepto',
'date_format' => 'jS M Y',
'excerpt_length' => '50',
'nav' => array(
'class' => 'nav',
'dropdown_li_class' => 'dropdown',
'dropdown_ul_class' => 'dropdown-menu'
)
),
'twig' => array(
'zepto.environment' => 'dev',
'zepto.content_dir' => 'content',
'zepto.plugins_dir' => 'plugins',
'zepto.templates_dir' => 'templates',
'zepto.default_template' => 'base.twig',
'zepto.content_ext' => array('.md', '.markdown'),
'zepto.plugins_enabled' => false,
'site.site_root' => 'http://localhost:8888/zepto/',
'site.site_title' => 'Zepto',
'site.date_format' => 'jS M Y',
'site.excerpt_length' => '50',
'twig' => array(
'charset' => 'utf-8',
'cache' => 'cache',
'strict_variables' => false,
Expand All @@ -80,7 +71,7 @@ public function testValidateConfig()
public function testConfigWithInvalidContentDir()
{
$config = Helper::default_config();
$config['zepto']['content_dir'] = 'no_such_dir';
$config['zepto.content_dir'] = 'no_such_dir';
Helper::validate_config($config);
}

Expand All @@ -91,7 +82,7 @@ public function testConfigWithInvalidContentDir()
public function testConfigWithInvalidPluginsDir()
{
$config = Helper::default_config();
$config['zepto']['plugins_dir'] = 'no_such_dir';
$config['zepto.plugins_dir'] = 'no_such_dir';
Helper::validate_config($config);
}

Expand All @@ -102,7 +93,7 @@ public function testConfigWithInvalidPluginsDir()
public function testConfigWithInvalidTemplatesDir()
{
$config = Helper::default_config();
$config['zepto']['templates_dir'] = 'no_such_dir';
$config['zepto.templates_dir'] = 'no_such_dir';
Helper::validate_config($config);
}

Expand All @@ -113,7 +104,7 @@ public function testConfigWithInvalidTemplatesDir()
public function testConfigWithInvalidDefaultTemplate()
{
$config = Helper::default_config();
$config['zepto']['default_template'] = 'no_such_file';
$config['zepto.default_template'] = 'no_such_file';
Helper::validate_config($config);
}

Expand All @@ -124,7 +115,7 @@ public function testConfigWithInvalidDefaultTemplate()
public function testConfigWithInvalidSiteRoot()
{
$config = Helper::default_config();
$config['zepto']['environment'] = 'production';
$config['zepto.environment'] = 'production';
$config['site']['site_root'] = 'fuck://this@should?fail';
Helper::validate_config($config);
}
Expand All @@ -138,7 +129,6 @@ public function testUrlFor()
$helper = new Helper($zepto->app);
$actual = $helper->url_for('index.md');
$expected = 'http://localhost:8888/zepto/';

$this->assertEquals($expected, $actual);
}

Expand All @@ -147,11 +137,12 @@ public function testUrlFor()
*/
public function testUrlForFailure()
{
ob_start();
$zepto = new Zepto;
$helper = new Helper($zepto->app);
$actual = $helper->url_for('non-index.md');

$this->assertNull($actual);
ob_end_clean();
}

/**
Expand All @@ -163,7 +154,6 @@ public function testLinkFor()
$helper = new Helper($zepto->app);
$actual = $helper->link_for('index.md');
$expected = '<a href="http://localhost:8888/zepto/"> Welcome </a>';

$this->assertEquals($expected, $actual);
}

Expand All @@ -172,11 +162,12 @@ public function testLinkFor()
*/
public function testLinkForFailure()
{
ob_start();
$zepto = new Zepto;
$helper = new Helper($zepto->app);
$actual = $helper->link_for('non-index.md');

$this->assertNull($actual);
ob_end_clean();
}

}

0 comments on commit c612b90

Please sign in to comment.