Skip to content

Commit

Permalink
Merge branch 'feature/4.0.0dev0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lytc committed Jul 8, 2014
2 parents 9b730d4 + 36388b7 commit cde3a75
Show file tree
Hide file tree
Showing 313 changed files with 13,172 additions and 1,864 deletions.
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -12,11 +12,11 @@
],
"require": {
"php": ">=5.4",
"doctrine/inflector": "v1.0"
"doctrine/inflector": "v1.0",
"ircmaxell/password-compat": "1.0.3"
},
"require-dev": {
"phpunit/phpunit": "4.0",
"mockery/mockery": "dev-master",
"phpunit/phpunit": "4.1.*",
"zendframework/zend-code": "2.3.*@dev",
"satooshi/php-coveralls": "dev-master"
},
Expand Down
8 changes: 3 additions & 5 deletions phpunit.xml
Expand Up @@ -5,13 +5,12 @@
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
cacheTokens="false"
colors="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
Expand All @@ -20,10 +19,9 @@
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
strict="false"
verbose="false">
strict="false">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
Expand Down
158 changes: 97 additions & 61 deletions src/Sloths/Application/Application.php
Expand Up @@ -2,24 +2,27 @@

namespace Sloths\Application;

use Sloths\Http\Response;
use Sloths\Application\Exception\Pass;
use Sloths\Application\Exception\NotFound;
use Sloths\Application\Exception\Stop;
use Sloths\Application\Service\ServiceInterface;
use Sloths\Misc\ConfigurableTrait;
use Sloths\Observer\ObserverTrait;
use \Closure;

/**
* Class Application
* @package Sloths\Application
*
* @property Service\Request $request
* @property Service\Response $response
* @property \Sloths\Http\Request $request
* @property \Sloths\Http\Response $response
* @property Service\View $view
* @property Service\Router $router
* @property Service\Config $config
* @property Service\Session $session
* @property Service\Messages $messages
* @property \Sloths\Misc\Config $config
* @property \Sloths\Session\Session $session
* @property \Sloths\Session\Messages $messages
* @property \Sloths\Translation\Translator $translator
* @property \Sloths\Application\Service\Validator $validator
*/
class Application
{
Expand Down Expand Up @@ -58,11 +61,14 @@ class Application
'config' => 'Sloths\Misc\Config',
'request' => 'Sloths\Http\Request',
'response' => 'Sloths\Http\Response',
'redirect' => 'Sloths\Application\Service\Redirect',
'view' => 'Sloths\Application\Service\View',
'router' => 'Sloths\Application\Service\Router',
'session' => 'Sloths\Session\Session',
'flash' => 'Sloths\Session\Flash',
'messages' => 'Sloths\Session\Messages'
'messages' => 'Sloths\Session\Messages',
'translator' => 'Sloths\Translation\Translator',
'validator' => 'Sloths\Application\Service\Validator'
];

/**
Expand All @@ -87,10 +93,10 @@ class Application
* @var array
*/
protected $shortcutProperties = [
'queryParams' => ['request', 'queryParams'],
'postParams' => ['request', 'postParams'],
'params' => ['request', 'params'],
'get' => ['request', 'get'],
'post' => ['request', 'post'],
'cookie' => ['request', 'cookie'],
'cookies' => ['request', 'cookies'],
'headers' => ['request', 'headers'],
];

Expand Down Expand Up @@ -515,37 +521,20 @@ public function setShortcutProperties(array $shortcuts)
}

/**
* @param $url
* @throws Exception\Stop
*/
public function redirectTo($url)
protected function stop()
{
call_user_func_array([$this->response, 'redirect'], func_get_args());
$this->response->send();
$this->stop();
}

/**
*
*/
public function redirectBack()
{
$this->redirectTo($this->request->getReferrer());
throw new Stop();
}

/**
*
*/
public function stop()
{
exit;
}

/**
* @throws NotFound
*/
public function notFound()
protected function notFound()
{
throw new NotFound();
$this->response->setStatusCode(404);
$this->stop();
}

/**
Expand Down Expand Up @@ -578,50 +567,97 @@ protected function boot()
}

/**
* @return $this|void
* @param Response $response
* @return $this
*/
public function run()
protected function send(Response $response)
{
if (false === $this->before()) {
return $this;
$headers = $response->getHeaders();

http_response_code($response->getStatusCode()?: 200);

if ($headerLocation = $headers->getLine('Location')) {
header($headerLocation);
return;
}

$this->boot();
$body = $response->getBody();
$isBodyString = is_string($body);

if ($this->notify('run') === false) {
return $this;
if (!$headers->get('Content-Type')) {
if ($isBodyString) {
$headers->set('Content-Type', 'text/html');
} else {
$headers->set('Content-Type', 'application/json');
}
}

$request = $this->request;
$method = $request->getMethod();
$requestPath = $request->getPath();
foreach ($headers->getLines() as $line) {
header($line);
}

if ($this->requestBasePath) {
$requestPath = substr($requestPath, strlen($this->requestBasePath))?: '/';
if (!$isBodyString) {
$body = json_encode($body);
}

$found = false;
while ($route = $this->router->matches($method, $requestPath)) {
$callback = $route->getCallback();
$callback = $callback->bindTo($this);
echo $body;
return $this;
}

try {
$result = call_user_func_array($callback, $route->getParams());
$this->response->setBody($result)->send();
$found = true;
break;
} catch(Pass $e) {
/**
* @return $this|void
*/
public function run()
{
$this->boot();

try {
$result = $this->before();

if ($result instanceof Response) {
$this->setService('response', $result);
$this->stop();
}
}

$this->after();
$this->notify('ran');
$result = $this->notify('run');

if ($result instanceof Response) {
$this->setService('response', $result);
$this->stop();
}

$request = $this->request;
$method = $request->getMethod();
$requestPath = $request->getPath();

if ($this->requestBasePath) {
$requestPath = substr($requestPath, strlen($this->requestBasePath))?: '/';
}

while ($route = $this->router->matches($method, $requestPath)) {
$callback = $route->getCallback();
$callback = $callback->bindTo($this, $this);

try {
$result = call_user_func_array($callback, $route->getParams());
if ($result instanceof Response) {
$this->setService('response', $result);
} else {
$this->response->setBody($result);
}
$this->stop();
} catch(Pass $e) {

}
}
$this->response->setStatusCode(404);
} catch(Stop $e) {

if ($found) {
return $this;
}

return $this->notFound();
$this->send($this->response);
$this->after();
$this->notify('ran');
return $this;
}
}
18 changes: 18 additions & 0 deletions src/Sloths/Application/Console.php
@@ -0,0 +1,18 @@
<?php

namespace Sloths\Application;

class Console
{
protected $application;

public function __construct(Application $application)
{
$this->application = $application;
}

public function run()
{

}
}
8 changes: 8 additions & 0 deletions src/Sloths/Application/Exception/Stop.php
@@ -0,0 +1,8 @@
<?php

namespace Sloths\Application\Exception;

class Stop extends \Exception
{

}
31 changes: 31 additions & 0 deletions src/Sloths/Application/Service/Redirect.php
@@ -0,0 +1,31 @@
<?php

namespace Sloths\Application\Service;

class Redirect implements ServiceInterface
{
use ServiceTrait;

/**
* @param string $url
* @param int $code
* @return \Sloths\Http\Response
*/
public function to($url, $code = 302)
{
$response = $this->application->response;
$response
->setStatusCode($code)
->getHeaders()->set('Location', $url);

return $response;
}

/**
* @return \Sloths\Http\Response
*/
public function back()
{
return $this->to($this->application->request->getReferrer());
}
}
9 changes: 9 additions & 0 deletions src/Sloths/Application/Service/ServiceTrait.php
Expand Up @@ -6,13 +6,22 @@

trait ServiceTrait
{
/**
* @var Application
*/
protected $application;

/**
* @param Application $application
*/
public function setApplication(Application $application)
{
$this->application = $application;
}

/**
* @return Application
*/
public function getApplication()
{
return $this->application;
Expand Down

0 comments on commit cde3a75

Please sign in to comment.