Skip to content

Commit

Permalink
Merge branch 'feature/catchable-exceptions' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Jan 20, 2013
2 parents 5e69e63 + 8b02366 commit d0023b5
Show file tree
Hide file tree
Showing 26 changed files with 373 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/Mustache/Compiler.php
Expand Up @@ -50,7 +50,7 @@ public function compile($source, array $tree, $name, $customEscape = false, $cha
/**
* Helper function for walking the Mustache token parse tree.
*
* @throws InvalidArgumentException upon encountering unknown token types.
* @throws Mustache_Exception_SyntaxException upon encountering unknown token types.
*
* @param array $tree Parse tree of Mustache tokens
* @param int $level (default: 0)
Expand Down Expand Up @@ -113,7 +113,7 @@ private function walk(array $tree, $level = 0)
break;

default:
throw new InvalidArgumentException('Unknown node type: '.json_encode($node));
throw new Mustache_Exception_SyntaxException(sprintf('Unknown token type: %s', $node[Mustache_Tokenizer::TYPE]), $node);
}
}

Expand Down Expand Up @@ -316,7 +316,7 @@ private function getFilters($id, $level)
const FILTER = '
$filter = $context->%s(%s);
if (is_string($filter) || !is_callable($filter)) {
throw new UnexpectedValueException(%s);
throw new Mustache_Exception_UnknownFilterException(%s);
}
$value = call_user_func($filter, $value);%s
';
Expand All @@ -338,7 +338,7 @@ private function getFilter(array $filters, $level)
$name = array_shift($filters);
$method = $this->getFindMethod($name);
$filter = ($method !== 'last') ? var_export($name, true) : '';
$msg = var_export(sprintf('Filter not found: %s', $name), true);
$msg = var_export($name, true);

return sprintf($this->prepare(self::FILTER, $level), $method, $filter, $msg, $this->getFilter($filters, $level));
}
Expand Down
22 changes: 13 additions & 9 deletions src/Mustache/Engine.php
Expand Up @@ -89,6 +89,8 @@ class Mustache_Engine
* 'logger' => new Mustache_StreamLogger('php://stderr'),
* );
*
* @throws Mustache_Exception_InvalidArgumentException If `escape` option is not callable.
*
* @param array $options (default: array())
*/
public function __construct(array $options = array())
Expand Down Expand Up @@ -123,7 +125,7 @@ public function __construct(array $options = array())

if (isset($options['escape'])) {
if (!is_callable($options['escape'])) {
throw new InvalidArgumentException('Mustache Constructor "escape" option must be callable');
throw new Mustache_Exception_InvalidArgumentException('Mustache Constructor "escape" option must be callable');
}

$this->escape = $options['escape'];
Expand Down Expand Up @@ -233,15 +235,15 @@ public function getPartialsLoader()
/**
* Set partials for the current partials Loader instance.
*
* @throws RuntimeException If the current Loader instance is immutable
* @throws Mustache_Exception_RuntimeException If the current Loader instance is immutable
*
* @param array $partials (default: array())
*/
public function setPartials(array $partials = array())
{
$loader = $this->getPartialsLoader();
if (!$loader instanceof Mustache_Loader_MutableLoader) {
throw new RuntimeException('Unable to set partials on an immutable Mustache Loader instance');
throw new Mustache_Exception_RuntimeException('Unable to set partials on an immutable Mustache Loader instance');
}

$loader->setTemplates($partials);
Expand All @@ -254,14 +256,14 @@ public function setPartials(array $partials = array())
* any other valid Mustache context value. They will be prepended to the context stack, so they will be available in
* any template loaded by this Mustache instance.
*
* @throws InvalidArgumentException if $helpers is not an array or Traversable
* @throws Mustache_Exception_InvalidArgumentException if $helpers is not an array or Traversable
*
* @param array|Traversable $helpers
*/
public function setHelpers($helpers)
{
if (!is_array($helpers) && !$helpers instanceof Traversable) {
throw new InvalidArgumentException('setHelpers expects an array of helpers');
throw new Mustache_Exception_InvalidArgumentException('setHelpers expects an array of helpers');
}

$this->getHelpers()->clear();
Expand Down Expand Up @@ -343,12 +345,14 @@ public function removeHelper($name)
/**
* Set the Mustache Logger instance.
*
* @throws Mustache_Exception_InvalidArgumentException If logger is not an instance of Mustache_Logger or Psr\Log\LoggerInterface.
*
* @param Mustache_Logger|Psr\Log\LoggerInterface $logger
*/
public function setLogger($logger = null)
{
if ($logger !== null && !($logger instanceof Mustache_Logger || is_a($logger, 'Psr\\Log\\LoggerInterface'))) {
throw new InvalidArgumentException('Expected an instance of Mustache_Logger or Psr\\Log\\LoggerInterface.');
throw new Mustache_Exception_InvalidArgumentException('Expected an instance of Mustache_Logger or Psr\\Log\\LoggerInterface.');
}

$this->logger = $logger;
Expand Down Expand Up @@ -636,7 +640,7 @@ private function getCacheFilename($source)
/**
* Helper method to dump a generated Mustache Template subclass to the file cache.
*
* @throws RuntimeException if unable to create the cache directory or write to $fileName.
* @throws Mustache_Exception_RuntimeException if unable to create the cache directory or write to $fileName.
*
* @param string $fileName
* @param string $source
Expand All @@ -655,7 +659,7 @@ private function writeCacheFile($fileName, $source)

@mkdir($dirName, 0777, true);
if (!is_dir($dirName)) {
throw new RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName));
throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName));
}

}
Expand All @@ -682,7 +686,7 @@ private function writeCacheFile($fileName, $source)
);
}

throw new RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Mustache/Exception.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* A Mustache Exception interface.
*/
interface Mustache_Exception
{
// This space intentionally left blank.
}
18 changes: 18 additions & 0 deletions src/Mustache/Exception/InvalidArgumentException.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Invalid argument exception.
*/
class Mustache_Exception_InvalidArgumentException extends InvalidArgumentException implements Mustache_Exception
{
// This space intentionally left blank.
}
18 changes: 18 additions & 0 deletions src/Mustache/Exception/LogicException.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Logic exception.
*/
class Mustache_Exception_LogicException extends LogicException implements Mustache_Exception
{
// This space intentionally left blank.
}
18 changes: 18 additions & 0 deletions src/Mustache/Exception/RuntimeException.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Runtime exception.
*/
class Mustache_Exception_RuntimeException extends RuntimeException implements Mustache_Exception
{
// This space intentionally left blank.
}
29 changes: 29 additions & 0 deletions src/Mustache/Exception/SyntaxException.php
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Mustache syntax exception.
*/
class Mustache_Exception_SyntaxException extends LogicException implements Mustache_Exception
{
protected $token;

public function __construct($msg, array $token)
{
$this->token = $token;
parent::__construct($msg);
}

public function getToken()
{
return $this->token;
}
}
29 changes: 29 additions & 0 deletions src/Mustache/Exception/UnknownFilterException.php
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Unknown filter exception.
*/
class Mustache_Exception_UnknownFilterException extends UnexpectedValueException implements Mustache_Exception
{
protected $filterName;

public function __construct($filterName)
{
$this->filterName = $filterName;
parent::__construct(sprintf('Unknown filter: %s', $filterName));
}

public function getFilterName()
{
return $this->filterName;
}
}
29 changes: 29 additions & 0 deletions src/Mustache/Exception/UnknownHelperException.php
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Unknown helper exception.
*/
class Mustache_Exception_UnknownHelperException extends InvalidArgumentException implements Mustache_Exception
{
protected $helperName;

public function __construct($helperName)
{
$this->helperName = $helperName;
parent::__construct(sprintf('Unknown helper: %s', $helperName));
}

public function getHelperName()
{
return $this->helperName;
}
}
29 changes: 29 additions & 0 deletions src/Mustache/Exception/UnknownTemplateException.php
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2013 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Unknown template exception.
*/
class Mustache_Exception_UnknownTemplateException extends InvalidArgumentException implements Mustache_Exception
{
protected $templateName;

public function __construct($templateName)
{
$this->templateName = $templateName;
parent::__construct(sprintf('Unknown template: %s', $templateName));
}

public function getTemplateName()
{
return $this->templateName;
}
}
12 changes: 7 additions & 5 deletions src/Mustache/HelperCollection.php
Expand Up @@ -21,15 +21,15 @@ class Mustache_HelperCollection
*
* Optionally accepts an array (or Traversable) of `$name => $helper` pairs.
*
* @throws InvalidArgumentException if the $helpers argument isn't an array or Traversable
* @throws Mustache_Exception_InvalidArgumentException if the $helpers argument isn't an array or Traversable
*
* @param array|Traversable $helpers (default: null)
*/
public function __construct($helpers = null)
{
if ($helpers !== null) {
if (!is_array($helpers) && !$helpers instanceof Traversable) {
throw new InvalidArgumentException('HelperCollection constructor expects an array of helpers');
throw new Mustache_Exception_InvalidArgumentException('HelperCollection constructor expects an array of helpers');
}

foreach ($helpers as $name => $helper) {
Expand Down Expand Up @@ -79,14 +79,16 @@ public function __get($name)
/**
* Get a helper by name.
*
* @throws Mustache_Exception_UnknownHelperException If helper does not exist.
*
* @param string $name
*
* @return mixed Helper
*/
public function get($name)
{
if (!$this->has($name)) {
throw new InvalidArgumentException('Unknown helper: '.$name);
throw new Mustache_Exception_UnknownHelperException($name);
}

return $this->helpers[$name];
Expand Down Expand Up @@ -133,14 +135,14 @@ public function __unset($name)
/**
* Check whether a given helper is present in the collection.
*
* @throws InvalidArgumentException if the requested helper is not present.
* @throws Mustache_Exception_UnknownHelperException if the requested helper is not present.
*
* @param string $name
*/
public function remove($name)
{
if (!$this->has($name)) {
throw new InvalidArgumentException('Unknown helper: '.$name);
throw new Mustache_Exception_UnknownHelperException($name);
}

unset($this->helpers[$name]);
Expand Down
4 changes: 3 additions & 1 deletion src/Mustache/Loader/ArrayLoader.php
Expand Up @@ -43,14 +43,16 @@ public function __construct(array $templates = array())
/**
* Load a Template.
*
* @throws Mustache_Exception_UnknownTemplateException If a template file is not found.
*
* @param string $name
*
* @return string Mustache Template source
*/
public function load($name)
{
if (!isset($this->templates[$name])) {
throw new InvalidArgumentException('Template '.$name.' not found.');
throw new Mustache_Exception_UnknownTemplateException($name);
}

return $this->templates[$name];
Expand Down

0 comments on commit d0023b5

Please sign in to comment.