Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Aug 26, 2012
2 parents 1175f3e + b6387dd commit 36ee83a
Show file tree
Hide file tree
Showing 58 changed files with 352 additions and 243 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: php

php:
- 5.3
- 5.4

script: phpunit --coverage-text
8 changes: 8 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ Here are more links that may also be useful.
* Road Map: <http://github.com/codeguy/Slim/wiki/Road-Map>
* Source Code: <http://github.com/codeguy/Slim/>

## Testing

Unit-tests can be run using PHPUnit.

```
$ phpunit
```

## About the Author

Slim is created and maintained by Josh Lockhart, a web developer by day at [New Media Campaigns](http://www.newmediacampaigns.com), and a [hacker by night](http://github.com/codeguy).
Expand Down
24 changes: 12 additions & 12 deletions Slim/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ class Slim_Log {
* @var array
*/
static protected $levels = array(
0 => 'FATAL',
1 => 'ERROR',
2 => 'WARN',
3 => 'INFO',
4 => 'DEBUG'
self::FATAL => 'FATAL',
self::ERROR => 'ERROR',
self::WARN => 'WARN',
self::INFO => 'INFO',
self::DEBUG => 'DEBUG'
);

/**
Expand All @@ -94,7 +94,7 @@ class Slim_Log {
public function __construct( $writer ) {
$this->writer = $writer;
$this->enabled = true;
$this->level = 4;
$this->level = self::DEBUG;
}

/**
Expand Down Expand Up @@ -170,7 +170,7 @@ public function isEnabled() {
* @return mixed|false What the Logger returns, or false if Logger not set or not enabled
*/
public function debug( $object ) {
return $this->log($object, 4);
return $this->log($object, self::DEBUG);
}

/**
Expand All @@ -179,7 +179,7 @@ public function debug( $object ) {
* @return mixed|false What the Logger returns, or false if Logger not set or not enabled
*/
public function info( $object ) {
return $this->log($object, 3);
return $this->log($object, self::INFO);
}

/**
Expand All @@ -188,7 +188,7 @@ public function info( $object ) {
* @return mixed|false What the Logger returns, or false if Logger not set or not enabled
*/
public function warn( $object ) {
return $this->log($object, 2);
return $this->log($object, self::WARN);
}

/**
Expand All @@ -197,7 +197,7 @@ public function warn( $object ) {
* @return mixed|false What the Logger returns, or false if Logger not set or not enabled
*/
public function error( $object ) {
return $this->log($object, 1);
return $this->log($object, self::ERROR);
}

/**
Expand All @@ -206,7 +206,7 @@ public function error( $object ) {
* @return mixed|false What the Logger returns, or false if Logger not set or not enabled
*/
public function fatal( $object ) {
return $this->log($object, 0);
return $this->log($object, self::FATAL);
}

/**
Expand All @@ -222,4 +222,4 @@ protected function log( $object, $level ) {
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion Slim/LogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
* @package Slim
* @author Josh Lockhart
* @since 1.5.2
* @since 1.6.0
*/
class Slim_LogWriter {
/**
Expand Down
2 changes: 1 addition & 1 deletion Slim/Middleware/ContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* @package Slim
* @author Josh Lockhart
* @since 1.5.2
* @since 1.6.0
*/
class Slim_Middleware_ContentTypes extends Slim_Middleware {
/**
Expand Down
2 changes: 1 addition & 1 deletion Slim/Middleware/Flash.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* @package Slim
* @author Josh Lockhart
* @since 1.5.2
* @since 1.6.0
*/
class Slim_Middleware_Flash extends Slim_Middleware implements ArrayAccess, IteratorAggregate {
/**
Expand Down
2 changes: 1 addition & 1 deletion Slim/Middleware/MethodOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
* @package Slim
* @author Josh Lockhart
* @since 1.5.2
* @since 1.6.0
*/
class Slim_Middleware_MethodOverride extends Slim_Middleware {
/**
Expand Down
1 change: 1 addition & 0 deletions Slim/Middleware/PrettyExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function call() {
} catch ( Exception $e ) {
$env = $this->app->environment();
$env['slim.log']->error($e);
$this->app->contentType('text/html');
$this->app->response()->status(500);
$this->app->response()->body($this->renderBody($env, $e));
}
Expand Down
2 changes: 1 addition & 1 deletion Slim/Middleware/SessionCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*
* @package Slim
* @author Josh Lockhart
* @since 1.5.2
* @since 1.6.0
*/
class Slim_Middleware_SessionCookie extends Slim_Middleware {
/**
Expand Down
16 changes: 13 additions & 3 deletions Slim/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,20 @@ class Slim_Router implements Iterator {
* Constructor
* @param string $resourceUri The request URI
*/
public function __construct( $resourceUri ) {
$this->resourceUri = $resourceUri;
public function __construct() {
$this->routes = array();
}

/**
* Set Resource URI
*
* This method injects the current request's resource URI, and should be invoked
* immediately before route dispatch iteration.
*/
public function setResourceUri($uri) {
$this->resourceUri = $uri;
}

/**
* Get Current Route
* @return Slim_Route|false
Expand Down Expand Up @@ -169,7 +178,7 @@ public function dispatch( Slim_Route $route ) {
//Invoke middleware
foreach ( $route->getMiddleware() as $mw ) {
if ( is_callable($mw) ) {
call_user_func($mw);
call_user_func_array($mw, array($route));
}
}

Expand All @@ -178,6 +187,7 @@ public function dispatch( Slim_Route $route ) {
call_user_func_array($route->getCallable(), array_values($route->getParams()));
return true;
}

return false;
}

Expand Down
66 changes: 33 additions & 33 deletions Slim/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,8 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//This determines which errors are reported by PHP. By default, all
//errors (including E_STRICT) are reported.
error_reporting(E_ALL | E_STRICT);

if ( !defined('MCRYPT_RIJNDAEL_256') ) {
define('MCRYPT_RIJNDAEL_256', 0);
}
if ( !defined('MCRYPT_MODE_CBC') ) {
define('MCRYPT_MODE_CBC', 0);
}

//This tells PHP to auto-load classes using Slim's autoloader; this will
//only auto-load a class file located in the same directory as Slim.php
//whose file name (excluding the final dot and extension) is the same
//as its class name (case-sensitive). For example, "View.php" will be
//loaded when Slim uses the "View" class for the first time.
spl_autoload_register(array('Slim', 'autoload'));

//PHP 5.3 will complain if you don't set a timezone. If you do not
//specify your own timezone before requiring Slim, this tells PHP to use UTC.
if ( @date_default_timezone_set(date_default_timezone_get()) === false ) {
date_default_timezone_set('UTC');
}
// Comment out this line if you are using an alternative autoloader (e.g. Composer)
Slim::registerAutoloader();

/**
* Slim
Expand All @@ -62,6 +41,11 @@
* @since 1.0.0
*/
class Slim {
/**
* @const string
*/
const VERSION = '1.6.0';

/**
* @var array[Slim]
*/
Expand Down Expand Up @@ -143,6 +127,13 @@ public static function autoload( $class ) {
}
}

/**
* Register Slim's built-in autoloader
*/
public static function registerAutoloader() {
spl_autoload_register(array('Slim', 'autoload'));
}

/***** INSTANTIATION *****/

/**
Expand All @@ -152,6 +143,7 @@ public static function autoload( $class ) {
*/
public function __construct( $userSettings = array() ) {
//Setup Slim application
$this->settings = array_merge(self::getDefaultSettings(), $userSettings);
$this->environment = Slim_Environment::getInstance();
$this->request = new Slim_Http_Request($this->environment);
$this->response = new Slim_Http_Response();
Expand Down Expand Up @@ -181,9 +173,6 @@ public function __construct( $userSettings = array() ) {
$log->setEnabled($this->config('log.enabled'));
$log->setLevel($this->config('log.level'));
$this->environment['slim.log'] = $log;

//Set global error handler
set_error_handler(array('Slim', 'handleErrors'));
}

/**
Expand Down Expand Up @@ -221,13 +210,14 @@ public function getName() {
*/
public static function getDefaultSettings() {
return array(
//Mode
//Application
'install_autoloader' => true,
'mode' => 'development',
//Debugging
'debug' => true,
//Logging
'log.writer' => null,
'log.level' => 4,
'log.level' => Slim_Log::DEBUG,
'log.enabled' => true,
//View
'templates.path' => './templates',
Expand Down Expand Up @@ -933,16 +923,15 @@ public function urlFor( $name, $params = array() ) {
* this issues a 302 Found response; this is considered the default
* generic redirect response. You may also specify another valid
* 3xx status code if you want. This method will automatically set the
* HTTP Location header for you using the URL parameter and place the
* destination URL into the response body.
* HTTP Location header for you using the URL parameter.
*
* @param string $url The destination URL
* @param int $status The HTTP redirect status code (Optional)
* @return void
*/
public function redirect( $url, $status = 302 ) {
$this->response->redirect($url, $status);
$this->halt($status, $url); }
$this->halt($status); }

/***** FLASH *****/

Expand Down Expand Up @@ -1091,6 +1080,8 @@ public function add( Slim_Middleware $newMiddleware ) {
* @return void
*/
public function run() {
set_error_handler(array('Slim', 'handleErrors'));

//Apply final outer middleware layers
$this->add(new Slim_Middleware_PrettyExceptions());

Expand Down Expand Up @@ -1120,6 +1111,8 @@ public function run() {

//Send body
echo $body;

restore_error_handler();
}

/**
Expand All @@ -1139,6 +1132,7 @@ public function call() {
$this->applyHook('slim.before.router');
$dispatched = false;
$httpMethodsAllowed = array();
$this->router->setResourceUri($this->request->getResourceUri());
$this->router->getMatchedRoutes();
foreach ( $this->router as $route ) {
if ( $route->supportsHttpMethod($this->environment['REQUEST_METHOD']) ) {
Expand All @@ -1152,12 +1146,18 @@ public function call() {
} catch ( Slim_Exception_Pass $e ) {
continue;
}
} else { $httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods()); }
} else {
$httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods());
}
}
if ( !$dispatched ) {
if ( $httpMethodsAllowed ) {
$this->response['Allow'] = implode(' ', $httpMethodsAllowed);
$this->halt(405, 'HTTP method not allowed for the requested resource. Use one of these instead: ' . implode(', ', $httpMethodsAllowed)); } else { $this->notFound(); } }
$this->halt(405, 'HTTP method not allowed for the requested resource. Use one of these instead: ' . implode(', ', $httpMethodsAllowed));
} else {
$this->notFound();
}
}
$this->applyHook('slim.after.router');
$this->stop();
} catch ( Slim_Exception_Stop $e ) {
Expand Down
2 changes: 1 addition & 1 deletion docs/caching-etag.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Setting an ETag with Slim is very simple. Invoke the `etag()` application method
echo "This will be cached after the initial request!";
});

Thats it. Make sure the unique ETag ID *is unique for the given resource*. Also make sure the ETag unique ID changes as your resource changes; otherwise, the HTTP client will continue serving its outdated cache.
That's it. Make sure the unique ETag ID *is unique for the given resource*. Also make sure the ETag unique ID changes as your resource changes; otherwise, the HTTP client will continue serving its outdated cache.
4 changes: 2 additions & 2 deletions docs/caching-last-modified.markdown
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Last-Modified [caching-last-modified] #

A Slim application provides built-in support for HTTP caching using the resources last modified date. When you specify a last modified date, Slim tells the HTTP client the date and time the current resource was last modified. The HTTP client will then send a **If-Modified-Since** header with each subsequent HTTP request for the given resource URI. If the last modification date you specify matches the **If-Modified-Since** HTTP request header, the Slim application will return a **304 Not Modified** HTTP response that will prompt the HTTP client to use its cache; this also prevents the Slim application from serving the entire markup for the resource URI saving bandwidth and response time.
A Slim application provides built-in support for HTTP caching using the resource's last modified date. When you specify a last modified date, Slim tells the HTTP client the date and time the current resource was last modified. The HTTP client will then send a **If-Modified-Since** header with each subsequent HTTP request for the given resource URI. If the last modification date you specify matches the **If-Modified-Since** HTTP request header, the Slim application will return a **304 Not Modified** HTTP response that will prompt the HTTP client to use its cache; this also prevents the Slim application from serving the entire markup for the resource URI saving bandwidth and response time.

Setting a last modified date with Slim is very simple. You only need to invoke the `lastModified()` application instance method in your route callback passing in a UNIX timestamp that represents the last modification date for the given resource. Be sure the `lastModified()` application instance method's timestamp updates along with the resources last modification date; otherwise, the browser client will continue serving its outdated cache.
Setting a last modified date with Slim is very simple. You only need to invoke the `lastModified()` application instance method in your route callback passing in a UNIX timestamp that represents the last modification date for the given resource. Be sure the `lastModified()` application instance method's timestamp updates along with the resource's last modification date; otherwise, the browser client will continue serving its outdated cache.

$app->get('/foo', function () use ($app) {
$app->lastModified(1286139652);
Expand Down
2 changes: 1 addition & 1 deletion docs/caching.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# HTTP Caching [caching] #

A Slim application provides built-in support for HTTP caching with its `etag()`, `lastModified()`, and `expires()` instance methods. It is best to use _one_ of `etag()` or `lastModified()` in conjunction with `expires()` per route; never use _both_ `etag()` and `lastModified()` together in the same route callback.
A Slim application provides built-in support for HTTP caching with its `etag()`, `lastModified()`, and `expires()` instance methods. It is best to use _one_ of `etag()` or `lastModified()` - in conjunction with `expires()` - per route; never use _both_ `etag()` and `lastModified()` together in the same route callback.

The `etag()` and `lastModified()` methods should be invoked in a route callback *before* other code; this allows Slim to check conditional GET requests _before_ processing the route callback's remaining code.

Expand Down
2 changes: 1 addition & 1 deletion docs/environment.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A Slim application's "environment" is an associative array of settings that are

When you instantiate a Slim application, the environment variables are derived from the `$_SERVER` superglobal; you do not need to set these yourself. However, you are free to modify or supplement these variables in [Slim middleware](#middleware).

These variables are fundamental to determining how your Slim application runs: the resource URI, the HTTP method, the HTTP request body, the URL query parameters, error output, and more. Middleware, described later, gives you the power to among other things — manipulate environment variables before and/or after the Slim application is run.
These variables are fundamental to determining how your Slim application runs: the resource URI, the HTTP method, the HTTP request body, the URL query parameters, error output, and more. Middleware, described later, gives you the power to - among other things - manipulate environment variables before and/or after the Slim application is run.

## Environment Variables ##

Expand Down
2 changes: 1 addition & 1 deletion docs/errors.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Error Handling [errors] #

Lets face it: sometimes things go wrong. It is important to intercept errors and respond to them appropriately. A Slim application provides helper methods to respond to errors and exceptions.
Let's face it: sometimes things go wrong. It is important to intercept errors and respond to them appropriately. A Slim application provides helper methods to respond to errors and exceptions.

Each Slim application handles its own errors and exceptions. If there are multiple Slim applications in the same PHP script, each application will catch and handle its own errors and exceptions. Errors or exceptions generated outside of a Slim application must be caught and handled elsewhere.

Expand Down
Loading

0 comments on commit 36ee83a

Please sign in to comment.