Skip to content

Commit

Permalink
Merge branch 'release-2.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Lockhart committed Nov 29, 2013
2 parents 3dec4f9 + b33f084 commit 02cf237
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 42 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ language: php
php:
- 5.3
- 5.4
- 5.5

script: phpunit --coverage-text
25 changes: 15 additions & 10 deletions Slim/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,10 @@ public function params($key = null)
* the value of the array key if requested; if the array key does not exist, NULL is returned.
*
* @param string $key
* @param mixed $default Default return value when key does not exist
* @return array|mixed|null
*/
public function get($key = null)
public function get($key = null, $default = null)
{
if (!isset($this->env['slim.request.query_hash'])) {
$output = array();
Expand All @@ -226,7 +227,7 @@ public function get($key = null)
if (isset($this->env['slim.request.query_hash'][$key])) {
return $this->env['slim.request.query_hash'][$key];
} else {
return null;
return $default;
}
} else {
return $this->env['slim.request.query_hash'];
Expand All @@ -240,10 +241,11 @@ public function get($key = null)
* the value of a hash key if requested; if the array key does not exist, NULL is returned.
*
* @param string $key
* @param mixed $default Default return value when key does not exist
* @return array|mixed|null
* @throws \RuntimeException If environment input is not available
*/
public function post($key = null)
public function post($key = null, $default = null)
{
if (!isset($this->env['slim.input'])) {
throw new \RuntimeException('Missing slim.input in environment variables');
Expand All @@ -266,7 +268,7 @@ public function post($key = null)
if (isset($this->env['slim.request.form_hash'][$key])) {
return $this->env['slim.request.form_hash'][$key];
} else {
return null;
return $default;
}
} else {
return $this->env['slim.request.form_hash'];
Expand All @@ -276,31 +278,34 @@ public function post($key = null)
/**
* Fetch PUT data (alias for \Slim\Http\Request::post)
* @param string $key
* @param mixed $default Default return value when key does not exist
* @return array|mixed|null
*/
public function put($key = null)
public function put($key = null, $default = null)
{
return $this->post($key);
return $this->post($key, $default);
}

/**
* Fetch PATCH data (alias for \Slim\Http\Request::post)
* @param string $key
* @param mixed $default Default return value when key does not exist
* @return array|mixed|null
*/
public function patch($key = null)
public function patch($key = null, $default = null)
{
return $this->post($key);
return $this->post($key, $default);
}

/**
* Fetch DELETE data (alias for \Slim\Http\Request::post)
* @param string $key
* @param mixed $default Default return value when key does not exist
* @return array|mixed|null
*/
public function delete($key = null)
public function delete($key = null, $default = null)
{
return $this->post($key);
return $this->post($key, $default);
}

/**
Expand Down
1 change: 1 addition & 0 deletions Slim/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class Response implements \ArrayAccess, \Countable, \IteratorAggregate
415 => '415 Unsupported Media Type',
416 => '416 Requested Range Not Satisfiable',
417 => '417 Expectation Failed',
418 => '418 I\'m a teapot',
422 => '422 Unprocessable Entity',
423 => '423 Locked',
//Server Error 5xx
Expand Down
35 changes: 20 additions & 15 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class Route

/**
* Constructor
* @param string $pattern The URL pattern (e.g. "/books/:id")
* @param mixed $callable Anything that returns TRUE for is_callable()
* @param string $pattern The URL pattern (e.g. "/books/:id")
* @param mixed $callable Anything that returns TRUE for is_callable()
*/
public function __construct($pattern, $callable)
{
Expand Down Expand Up @@ -154,6 +154,11 @@ public function getCallable()
*/
public function setCallable($callable)
{
$matches = array();
if (is_string($callable) && preg_match('!^([^\:]+)\:([[:alnum:]]+)$!', $callable, $matches)) {
$callable = array(new $matches[1], $matches[2]);
}

if (!is_callable($callable)) {
throw new \InvalidArgumentException('Route callable must be callable');
}
Expand Down Expand Up @@ -194,7 +199,7 @@ public function getName()
*/
public function setName($name)
{
$this->name = (string) $name;
$this->name = (string)$name;
}

/**
Expand All @@ -217,7 +222,7 @@ public function setParams($params)

/**
* Get route parameter value
* @param string $index Name of URL parameter
* @param string $index Name of URL parameter
* @return string
* @throws \InvalidArgumentException If route parameter does not exist at index
*/
Expand All @@ -232,8 +237,8 @@ public function getParam($index)

/**
* Set route parameter value
* @param string $index Name of URL parameter
* @param mixed $value The new parameter value
* @param string $index Name of URL parameter
* @param mixed $value The new parameter value
* @throws \InvalidArgumentException If route parameter does not exist at index
*/
public function setParam($index, $value)
Expand Down Expand Up @@ -351,7 +356,7 @@ public function matches($resourceUri)
$patternAsRegex = preg_replace_callback(
'#:([\w]+)\+?#',
array($this, 'matchesCallback'),
str_replace(')', ')?', (string) $this->pattern)
str_replace(')', ')?', (string)$this->pattern)
);
if (substr($this->pattern, -1) === '/') {
$patternAsRegex .= '?';
Expand All @@ -363,7 +368,7 @@ public function matches($resourceUri)
}
foreach ($this->paramNames as $name) {
if (isset($paramValues[$name])) {
if (isset($this->paramNamesPath[ $name ])) {
if (isset($this->paramNamesPath[$name])) {
$this->params[$name] = explode('/', urldecode($paramValues[$name]));
} else {
$this->params[$name] = urldecode($paramValues[$name]);
Expand All @@ -376,17 +381,17 @@ public function matches($resourceUri)

/**
* Convert a URL parameter (e.g. ":id", ":id+") into a regular expression
* @param array $m URL parameters
* @param array $m URL parameters
* @return string Regular expression for URL parameter
*/
protected function matchesCallback($m)
{
$this->paramNames[] = $m[1];
if (isset($this->conditions[ $m[1] ])) {
return '(?P<' . $m[1] . '>' . $this->conditions[ $m[1] ] . ')';
if (isset($this->conditions[$m[1]])) {
return '(?P<' . $m[1] . '>' . $this->conditions[$m[1]] . ')';
}
if (substr($m[0], -1) === '+') {
$this->paramNamesPath[ $m[1] ] = 1;
$this->paramNamesPath[$m[1]] = 1;

return '(?P<' . $m[1] . '>.+)';
}
Expand All @@ -396,7 +401,7 @@ protected function matchesCallback($m)

/**
* Set route name
* @param string $name The name of the route
* @param string $name The name of the route
* @return \Slim\Route
*/
public function name($name)
Expand All @@ -408,7 +413,7 @@ public function name($name)

/**
* Merge route conditions
* @param array $conditions Key-value array of URL parameter conditions
* @param array $conditions Key-value array of URL parameter conditions
* @return \Slim\Route
*/
public function conditions(array $conditions)
Expand All @@ -434,6 +439,6 @@ public function dispatch()
}

$return = call_user_func_array($this->getCallable(), array_values($this->getParams()));
return ($return === false)? false : true;
return ($return === false) ? false : true;
}
}
17 changes: 12 additions & 5 deletions Slim/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@

/**
* Slim
* @package Slim
* @author Josh Lockhart
* @since 1.0.0
* @package Slim
* @author Josh Lockhart
* @since 1.0.0
*
* @property \Slim\Environment $environment
* @property \Slim\Http\Response $response
* @property \Slim\Http\Request $request
* @property \Slim\Router $router
*/
class Slim
{
Expand Down Expand Up @@ -172,8 +177,11 @@ public function __construct(array $userSettings = array())
// Default view
$this->container->singleton('view', function ($c) {
$viewClass = $c['settings']['view'];
$templatesPath = $c['settings']['templates.path'];

return ($viewClass instanceOf \Slim\View) ? $viewClass : new $viewClass;
$view = ($viewClass instanceOf \Slim\View) ? $viewClass : new $viewClass;
$view->setTemplatesDirectory($templatesPath);
return $view;
});

// Default log writer
Expand Down Expand Up @@ -739,7 +747,6 @@ public function render($template, $data = array(), $status = null)
if (!is_null($status)) {
$this->response->status($status);
}
$this->view->setTemplatesDirectory($this->config('templates.path'));
$this->view->appendData($data);
$this->view->display($template);
}
Expand Down
24 changes: 15 additions & 9 deletions Slim/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,38 +236,44 @@ public function getTemplatePathname($file)
* This method echoes the rendered template to the current output buffer
*
* @param string $template Pathname of template file relative to templates directory
* @param array $data Any additonal data to be passed to the template.
*/
public function display($template)
public function display($template, $data = null)
{
echo $this->fetch($template);
echo $this->fetch($template, $data);
}

/**
* Return the contents of a rendered template file
* @var string $template The template pathname, relative to the template base directory
* @return string The rendered template
*
* @param string $template The template pathname, relative to the template base directory
* @param array $data Any additonal data to be passed to the template.
* @return string The rendered template
*/
public function fetch($template)
public function fetch($template, $data = null)
{
return $this->render($template);
return $this->render($template, $data);
}

/**
* Render a template file
*
* NOTE: This method should be overridden by custom view subclasses
*
* @var string $template The template pathname, relative to the template base directory
* @param string $template The template pathname, relative to the template base directory
* @param array $data Any additonal data to be passed to the template.
* @return string The rendered template
* @throws \RuntimeException If resolved template pathname is not a valid file
*/
protected function render($template)
protected function render($template, $data = null)
{
$templatePathname = $this->getTemplatePathname($template);
if (!is_file($templatePathname)) {
throw new \RuntimeException("View cannot render `$template` because the template does not exist");
}
extract($this->data->all());

$data = array_merge($this->data->all(), (array) $data);
extract($data);
ob_start();
require $templatePathname;

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
}
],
"require": {
"php": ">=5.3.0",
"ext-mcrypt": "*"
"php": ">=5.3.0"
},
"suggest": {
"ext-mcrypt": "Required for HTTP cookie encryption"
},
"autoload": {
"psr-0": { "Slim": "." }
Expand Down
7 changes: 7 additions & 0 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function testGet()
$this->assertEquals(3, count($req->get()));
$this->assertEquals('1', $req->get('one'));
$this->assertNull($req->get('foo'));
$this->assertFalse($req->get('foo', false));
}

/**
Expand All @@ -236,6 +237,7 @@ public function testGetWithoutMultibyte()
$this->assertEquals(3, count($req->get()));
$this->assertEquals('1', $req->get('one'));
$this->assertNull($req->get('foo'));
$this->assertFalse($req->get('foo', false));
}

/**
Expand All @@ -253,6 +255,7 @@ public function testPost()
$this->assertEquals(2, count($req->post()));
$this->assertEquals('bar', $req->post('foo'));
$this->assertNull($req->post('xyz'));
$this->assertFalse($req->post('xyz', false));
}

/**
Expand All @@ -271,6 +274,7 @@ public function testPostWithoutMultibyte()
$this->assertEquals(2, count($req->post()));
$this->assertEquals('bar', $req->post('foo'));
$this->assertNull($req->post('xyz'));
$this->assertFalse($req->post('xyz', false));
}

/**
Expand Down Expand Up @@ -319,6 +323,7 @@ public function testPut()
$this->assertEquals('bar', $req->put('foo'));
$this->assertEquals('bar', $req->params('foo'));
$this->assertNull($req->put('xyz'));
$this->assertFalse($req->put('xyz', false));
}

/**
Expand All @@ -337,6 +342,7 @@ public function testPatch()
$this->assertEquals('bar', $req->patch('foo'));
$this->assertEquals('bar', $req->params('foo'));
$this->assertNull($req->patch('xyz'));
$this->assertFalse($req->patch('xyz', false));
}

/**
Expand All @@ -355,6 +361,7 @@ public function testDelete()
$this->assertEquals('bar', $req->delete('foo'));
$this->assertEquals('bar', $req->params('foo'));
$this->assertNull($req->delete('xyz'));
$this->assertFalse($req->delete('xyz', false));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Middleware/SessionCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function testUnserializeErrorsAreCaughtAndLogged()

$logWriter->expects($this->once())
->method('write');
$oldLevel = error_reporting(E_ALL);

$app = new \Slim\Slim(array(
'log.writer' => $logWriter
Expand All @@ -149,6 +150,8 @@ public function testUnserializeErrorsAreCaughtAndLogged()
$mw->setNextMiddleware($app);
$mw->call();
$this->assertEquals(array(), $_SESSION);

error_reporting($oldLevel);
}

/**
Expand Down
Loading

0 comments on commit 02cf237

Please sign in to comment.