Skip to content

Commit

Permalink
Cleaning up various bootstrappers.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 16, 2016
1 parent 532d114 commit a00b601
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 118 deletions.
40 changes: 18 additions & 22 deletions src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class DetectEnvironment
*/
public function bootstrap(Application $app)
{
if (! $app->configurationIsCached()) {
$this->checkForSpecificEnvironmentFile($app);
if ($app->configurationIsCached()) {
return;
}

try {
(new Dotenv($app->environmentPath(), $app->environmentFile()))->load();
} catch (InvalidPathException $e) {
//
}
$this->checkForSpecificEnvironmentFile($app);

try {
(new Dotenv($app->environmentPath(), $app->environmentFile()))->load();
} catch (InvalidPathException $e) {
//
}
}

Expand All @@ -36,25 +38,19 @@ public function bootstrap(Application $app)
*/
protected function checkForSpecificEnvironmentFile($app)
{
if (php_sapi_name() == 'cli') {
$input = new ArgvInput;

if ($input->hasParameterOption('--env')) {
$file = $app->environmentFile().'.'.$input->getParameterOption('--env');

$this->loadEnvironmentFile($app, $file);
}
if (php_sapi_name() == 'cli' && with($input = new ArgvInput)->hasParameterOption('--env')) {
$this->setEnvironmentFilePath(
$app, $app->environmentFile().'.'.$input->getParameterOption('--env')
);
}

if (! env('APP_ENV')) {
if (! env('APP_ENV') || empty($file)) {
return;
}

if (empty($file)) {
$file = $app->environmentFile().'.'.env('APP_ENV');

$this->loadEnvironmentFile($app, $file);
}
$this->setEnvironmentFilePath(
$app, $app->environmentFile().'.'.env('APP_ENV')
);
}

/**
Expand All @@ -64,7 +60,7 @@ protected function checkForSpecificEnvironmentFile($app)
* @param string $file
* @return void
*/
protected function loadEnvironmentFile($app, $file)
protected function setEnvironmentFilePath($app, $file)
{
if (file_exists($app->environmentPath().'/'.$file)) {
$app->loadEnvironmentFrom($file);
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use ErrorException;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Debug\Exception\FatalErrorException;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function bootstrap(Application $app)
}

/**
* Convert a PHP error to an ErrorException.
* Convert PHP errors to ErrorException instances.
*
* @param int $level
* @param string $message
Expand Down Expand Up @@ -141,7 +142,7 @@ protected function fatalExceptionFromError(array $error, $traceOffset = null)
*/
protected function isFatal($type)
{
return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
return in_array($type, [E_COMPILE_ERROR, E_CORE_ERROR, E_ERROR, E_PARSE]);
}

/**
Expand All @@ -151,6 +152,6 @@ protected function isFatal($type)
*/
protected function getExceptionHandler()
{
return $this->app->make('Illuminate\Contracts\Debug\ExceptionHandler');
return $this->app->make(ExceptionHandler::class);
}
}
28 changes: 3 additions & 25 deletions src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function bootstrap(Application $app)
return $config->get('app.env', 'production');
});

date_default_timezone_set($config['app.timezone']);
date_default_timezone_set($config->get('app.timezone', 'UTC'));

mb_internal_encoding('UTF-8');
}
Expand Down Expand Up @@ -71,32 +71,10 @@ protected function getConfigurationFiles(Application $app)
{
$files = [];

$configPath = realpath($app->configPath());

foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) {
$nesting = $this->getConfigurationNesting($file, $configPath);

$files[$nesting.basename($file->getRealPath(), '.php')] = $file->getRealPath();
foreach (Finder::create()->files()->name('*.php')->in(realpath($app->configPath())) as $file) {
$files[basename($file->getRealPath(), '.php')] = $file->getRealPath();
}

return $files;
}

/**
* Get the configuration file nesting path.
*
* @param \Symfony\Component\Finder\SplFileInfo $file
* @param string $configPath
* @return string
*/
protected function getConfigurationNesting(SplFileInfo $file, $configPath)
{
$directory = $file->getPath();

if ($tree = trim(str_replace($configPath, '', $directory), DIRECTORY_SEPARATOR)) {
$tree = str_replace(DIRECTORY_SEPARATOR, '.', $tree).'.';
}

return $tree;
}
}
6 changes: 3 additions & 3 deletions src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class SetRequestForConsole
*/
public function bootstrap(Application $app)
{
$url = $app->make('config')->get('app.url', 'http://localhost');

$app->instance('request', Request::create($url, 'GET', [], [], [], $_SERVER));
$app->instance('request', Request::create(
$app->make('config')->get('app.url', 'http://localhost'), 'GET', [], [], [], $_SERVER
));
}
}
128 changes: 63 additions & 65 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,31 @@ protected function shouldntReport(Exception $e)
{
$dontReport = array_merge($this->dontReport, [HttpResponseException::class]);

foreach ($dontReport as $type) {
if ($e instanceof $type) {
return true;
}
return ! is_null(collect($dontReport)->first(function ($type) use ($e) {
return $e instanceof $type;
}));
}

/**
* Render an exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Exception $e)
{
$e = $this->prepareException($e);

if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof ValidationException) {
return $this->convertValidationExceptionToResponse($e, $request);
}

return false;
return $this->prepareResponse($request, $e);
}

/**
Expand All @@ -119,25 +137,27 @@ protected function prepareException(Exception $e)
}

/**
* Render an exception into a response.
* Create a response object from the given validation exception.
*
* @param \Illuminate\Validation\ValidationException $e
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Exception $e)
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
$e = $this->prepareException($e);
if ($e->response) {
return $e->response;
}

if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof ValidationException) {
return $this->convertValidationExceptionToResponse($e, $request);
$errors = $e->validator->errors()->getMessages();

if ($request->expectsJson()) {
return response()->json($errors, 422);
}

return $this->prepareResponse($request, $e);
return redirect()->back()->withInput(
$request->input()
)->withErrors($errors);
}

/**
Expand All @@ -156,36 +176,6 @@ protected function prepareResponse($request, Exception $e)
}
}

/**
* Map exception into an illuminate response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
protected function toIlluminateResponse($response, Exception $e)
{
if ($response instanceof SymfonyRedirectResponse) {
$response = new RedirectResponse($response->getTargetUrl(), $response->getStatusCode(), $response->headers->all());
} else {
$response = new Response($response->getContent(), $response->getStatusCode(), $response->headers->all());
}

return $response->withException($e);
}

/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Exception $e
* @return void
*/
public function renderForConsole($output, Exception $e)
{
(new ConsoleApplication)->renderException($e, $output);
}

/**
* Render the given HttpException.
*
Expand All @@ -204,40 +194,48 @@ protected function renderHttpException(HttpException $e)
}

/**
* Create a response object from the given validation exception.
* Create a Symfony response for the given exception.
*
* @param \Illuminate\Validation\ValidationException $e
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
protected function convertExceptionToResponse(Exception $e)
{
if ($e->response) {
return $e->response;
}

$errors = $e->validator->errors()->getMessages();
$e = FlattenException::create($e);

if ($request->expectsJson()) {
return response()->json($errors, 422);
}
$handler = new SymfonyExceptionHandler(config('app.debug'));

return redirect()->back()->withInput($request->input())->withErrors($errors);
return SymfonyResponse::create($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders());
}

/**
* Create a Symfony response for the given exception.
* Map the given exception into an Illuminate response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
* @return \Illuminate\Http\Response
*/
protected function convertExceptionToResponse(Exception $e)
protected function toIlluminateResponse($response, Exception $e)
{
$e = FlattenException::create($e);
if ($response instanceof SymfonyRedirectResponse) {
$response = new RedirectResponse($response->getTargetUrl(), $response->getStatusCode(), $response->headers->all());
} else {
$response = new Response($response->getContent(), $response->getStatusCode(), $response->headers->all());
}

$handler = new SymfonyExceptionHandler(config('app.debug'));
return $response->withException($e);
}

return SymfonyResponse::create($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders());
/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Exception $e
* @return void
*/
public function renderForConsole($output, Exception $e)
{
(new ConsoleApplication)->renderException($e, $output);
}

/**
Expand Down

0 comments on commit a00b601

Please sign in to comment.