Skip to content

Commit

Permalink
Adding some commodity shortcuts to View and adding a fallback for Par…
Browse files Browse the repository at this point in the history
…ameters
  • Loading branch information
woxxy committed Dec 26, 2012
1 parent 126e0b7 commit f625ac2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ Parameter managers are used to consistently store variables for use in the theme

Returns an array of parameters.

* __$pm->getParam($key)__
* __$pm->getParam($key, $fallback = undefined)__

* string _$key_ - The key for the value
* mixed _$fallback_

Returns the value stored with $key.
Returns the value stored with $key. The fallback is activated through func_num_args() so it works even when using `null`.

__Throws:__ _\OutOfBoundsException_ - If the value was not set
__Throws:__ _\OutOfBoundsException_ - If the value was not set and a fallback is not available

* __$pm->setParam($key, $value)__

Expand All @@ -116,6 +117,8 @@ Parameter managers are used to consistently store variables for use in the theme

The View returned by the Builder is usually a custom object extending \Foolz\Theme\View.

There's also a bunch of shortcuts to go to the above levels available (getBuilder, getBuilderParamManager, getTheme, getAssetManager)

__It's compulsory to override the `toString()` method (not the `__toString()` magic method!) in order to output the HTML. This function should output HTML or return a string.__

* __$view->toString()__
Expand Down
14 changes: 13 additions & 1 deletion classes/Foolz/Theme/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,26 @@ public function getLayout()
*/
public function getPartial($name)
{
if (isset($this->partials[$name]))
if (array_key_exists($name, $this->partials))
{
return $this->partials[$name];
}

throw new \OutOfBoundsException('No such partial exists.');
}

/**
* Tells if a partial has already been created
*
* @param string $name The name of the partial
*
* @return bool True if the partial has been set, false otherwise
*/
public function isPartial($name)
{
return array_key_exists($name, $this->partials);
}

/**
* Shorthand for building the layout
*
Expand Down
15 changes: 11 additions & 4 deletions classes/Foolz/Theme/ParamManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function getParams()
}

/**
* Returns the parameter with the key
* Returns the parameter with the key. Supports fallback.
* It uses func_num_args() so if a second parameter is passed, it will use it as fallback.
*
* @param string $key
* @return mixed The value of the parameter
Expand All @@ -42,12 +43,18 @@ public function getParams()
*/
public function getParam($key)
{
if ( ! array_key_exists($key, $this->params))
if (array_key_exists($key, $this->params))
{
throw new \OutOfBoundsException('Undefined parameter.');
return $this->params[$key];
}

return $this->params[$key];
// return a second parameter
if (func_num_args() === 2)
{
return func_get_arg(1);
}

throw new \OutOfBoundsException('Undefined parameter.');
}

/**
Expand Down
62 changes: 35 additions & 27 deletions classes/Foolz/Theme/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class View
* @param \Foolz\Theme\Builder $builder The Builder object creating this view
* @param string $type The type of view, it can be partial or layout
* @param string $view The name of the view
*
* @return \Foolz\Theme\View The new view
*/
public static function forge(\Foolz\Theme\Builder $builder, $type, $view)
{
Expand Down Expand Up @@ -89,6 +91,36 @@ public function getBuilder()
return $this->builder;
}

/**
* Returns the global parameter manager located in the builder
*
* @return \Foolz\Theme\ParamManager The ParamManager that belongs to the Builder
*/
public function getBuilderParamManager()
{
return $this->getBuilder()->getParamManager();
}

/**
* Returns the theme that belongs to this view
*
* @return \Foolz\Theme\Theme The Theme that created this View
*/
public function getTheme()
{
return $this->getBuilder()->getTheme();
}

/**
* Returns the asset manager that belongs to the theme that created this view
*
* @return \Foolz\Theme\AssetManager The Theme's AssetManager
*/
public function getAssetManager()
{
return $this->getTheme()->getAssetManager();
}

/**
* Set the type of view
*
Expand Down Expand Up @@ -189,36 +221,12 @@ public function doBuild()
}

/**
* If not extended, it will check if there's a classic view file and return the result
* Method to override to echo the content of the theme
*
* @return string The compiled string
* @throws \OutOfBoundsException If the view is not found also in the extended themes
* @throws \BadMethodCallException If not overridden
*/
public function toString()
{
$theme = $this->getBuilder()->getTheme();

while (true)
{
$file = $theme->getDir().$this->type.DIRECTORY_SEPARATOR.$this->view.'.php';
if (file_exists($file))
{
// isolation function
$function = function()
{
ob_start();
include $this->getTheme()->getDir().$this->type.DIRECTORY_SEPARATOR.$this->view.'.php';
return ob_get_clean();
};

$function = $function->bindTo($this);
return $function();
}

$theme = $theme->getExtended();
}

// this should be thrown by $theme->getExtended, but we want to be explicit
throw new \OutOfBoundsException;
throw new \BadMethodCallException('The toString() method must be overridden to output the theme content');
}
}

0 comments on commit f625ac2

Please sign in to comment.