Skip to content

Commit

Permalink
refactoring various classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 22, 2011
1 parent b0ffc01 commit 994949c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
favicon.*
.DS_Store
favicon.*
4 changes: 2 additions & 2 deletions laravel/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static function load($class)
protected static function find($class)
{
// After PHP namespaces were introduced, most libaries ditched underscores for
// namespaces to indicate the class directory hierarchy. We will check for the
// presence of namespace slashes to determine the directory separator.
// for namespaces to indicate the class directory hierarchy. We will check for
// the presence of namespace slashes to determine the directory separator.
$separator = (strpos($class, '\\') !== false) ? '\\' : '_';

$library = substr($class, 0, strpos($class, $separator));
Expand Down
2 changes: 1 addition & 1 deletion laravel/laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
*/
if (Config::$items['session']['driver'] !== '')
{
IoC::core('session')->save($driver);
IoC::core('session')->save();
}

$response->send();
25 changes: 19 additions & 6 deletions laravel/routing/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function find($name)
{
if (array_key_exists($name, $this->names)) return $this->names[$name];

// To find a named route, we need to iterate through every route defined
// for the application. We will cache the routes by name so we can load
// them very quickly if we need to find them a second time.
foreach ($this->loader->everything() as $key => $value)
{
if (is_array($value) and isset($value['name']) and $value['name'] === $name)
Expand Down Expand Up @@ -180,13 +183,16 @@ protected function controller($method, $uri, $destination)

if ( ! is_null($key = $this->controller_key($segments)))
{
// Extract the controller name from the URI segments.
// Extract the various parts of the controller call from the URI.
// First, we'll extract the controller name, then, since we need
// to extract the method and parameters, we will remove the name
// of the controller from the URI. Then we can shift the method
// off of the array of segments. Any remaining segments are the
// parameters that should be passed to the controller method.
$controller = implode('.', array_slice($segments, 0, $key));

// Remove the controller name from the URI.
$segments = array_slice($segments, $key);

// Extract the controller method from the remaining segments.
$method = (count($segments) > 0) ? array_shift($segments) : 'index';

return new Route($destination, $controller.'@'.$method, $segments);
Expand All @@ -206,6 +212,9 @@ protected function controller($method, $uri, $destination)
*/
protected function controller_key($segments)
{
// To find the proper controller, we need to iterate backwards through
// the URI segments and take the first file that matches. That file
// should be the deepest controller matched by the URI.
foreach (array_reverse($segments, true) as $key => $value)
{
$controller = implode('/', array_slice($segments, 0, $key + 1)).EXT;
Expand All @@ -225,14 +234,14 @@ protected function controller_key($segments)
*/
protected function wildcards($key)
{
$replacements = 0;
$count = 0;

// For optional parameters, first translate the wildcards to their
// regex equivalent, sans the ")?" ending. We will add the endings
// back on after we know how many replacements we made.
$key = str_replace(array_keys($this->optional), array_values($this->optional), $key, $replacements);
$key = str_replace(array_keys($this->optional), array_values($this->optional), $key, $count);

$key .= ($replacements > 0) ? str_repeat(')?', $replacements) : '';
$key .= ($count > 0) ? str_repeat(')?', $count) : '';

return str_replace(array_keys($this->patterns), array_values($this->patterns), $key);
}
Expand All @@ -254,6 +263,10 @@ protected function parameters($uri, $route)

$parameters = array();

// To find the parameters that should be passed to the route, we will
// iterate through the route segments, and if the segment is enclosed
// in parentheses, we will take the matching segment from the request
// URI and add it to the array of parameters.
for ($i = 0; $i < $count; $i++)
{
if (preg_match('/\(.+\)/', $route[$i]) and isset($uri[$i]))
Expand Down
37 changes: 25 additions & 12 deletions laravel/session/payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,32 @@ class Payload {
protected $exists = true;

/**
* Start the session handling for the current request.
* The session driver used to retrieve and store the session payload.
*
* @var Driver
*/
protected $driver;

/**
* Create a new session payload instance.
*
* @param Driver $driver
* @return void
*/
public function __construct(Driver $driver)
{
$this->driver = $driver;
}

/**
* Load the session for the current request.
*
* @param string $id
* @return void
*/
public function __construct(Driver $driver, $id)
public function load($id)
{
if ( ! is_null($id))
{
$this->session = $driver->load($id);
}
if ( ! is_null($id)) $this->session = $this->driver->load($id);

// If the session doesn't exist or is invalid, we will create a new session
// array and mark the session as being non-existent. Some drivers, such as
Expand All @@ -64,7 +78,7 @@ public function __construct(Driver $driver, $id)
if ( ! $this->has('csrf_token'))
{
$this->put('csrf_token', Str::random(40));
}
}
}

/**
Expand Down Expand Up @@ -236,18 +250,17 @@ public function token()
/**
* Store the session payload in storage.
*
* @param Driver $driver
* @return void
*/
public function save(Driver $driver)
public function save()
{
$this->session['last_activity'] = time();

$this->age();

$config = Config::$items['session'];

$driver->save($this->session, $config, $this->exists);
$this->driver->save($this->session, $config, $this->exists);

$this->cookie();

Expand All @@ -258,9 +271,9 @@ public function save(Driver $driver)
// occuring is controlled by the "sweepage" configuration option.
$sweepage = $config['sweepage'];

if ($driver instanceof Sweeper and (mt_rand(1, $sweepage[1]) <= $sweepage[0]))
if ($this->driver instanceof Sweeper and (mt_rand(1, $sweepage[1]) <= $sweepage[0]))
{
$driver->sweep(time() - ($config['lifetime'] * 60));
$this->driver->sweep(time() - ($config['lifetime'] * 60));
}
}

Expand Down
10 changes: 5 additions & 5 deletions laravel/validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ protected function validate_unique($attribute, $value, $parameters)
}

/**
* Validate that an attribute is a valid e-mail address.
* Validate than an attribute is a valid e-mail address.
*
* @param string $attribute
* @param mixed $value
Expand All @@ -406,7 +406,7 @@ protected function validate_email($attribute, $value)
}

/**
* Validate that an attribute is a valid URL.
* Validate than an attribute is a valid URL.
*
* @param string $attribute
* @param mixed $value
Expand Down Expand Up @@ -444,7 +444,7 @@ protected function validate_image($attribute, $value)
}

/**
* Validate that an attribute contains only alphabetic characters.
* Validate than an attribute contains only alphabetic characters.
*
* @param string $attribute
* @param mixed $value
Expand All @@ -456,7 +456,7 @@ protected function validate_alpha($attribute, $value)
}

/**
* Validate that an attribute contains only alpha-numeric characters.
* Validate than an attribute contains only alpha-numeric characters.
*
* @param string $attribute
* @param mixed $value
Expand All @@ -468,7 +468,7 @@ protected function validate_alpha_num($attribute, $value)
}

/**
* Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
* Validate than an attribute contains only alpha-numeric characters, dashes, and underscores.
*
* @param string $attribute
* @param mixed $value
Expand Down

0 comments on commit 994949c

Please sign in to comment.