Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass settings to Pimple #1457

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions Slim/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ final class Container extends PimpleContainer implements ContainerInterface
/**
* Create new container
*
* @param array $userSettings Associative array of application settings
* @param array $values The parameters or objects.
*/
public function __construct(array $userSettings = [])
public function __construct(array $values = [])
{
parent::__construct();
parent::__construct($values);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it me or would we be passing settings to Pimple then replacing it right after on line 71?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it does appear that it will get passed in twice. but only the ['settings'] part of the user provided array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's laziness on my part to avoid changing the default settings merging. Will see if I can tidy it up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah leave it, its probably quicker to replace it twice than to pop it off the array. Will merge, you can cleanup later if you wish.


$userSettings = isset($values['settings']) ? $values['settings'] : [];
$this->registerDefaultServices($userSettings);
}

Expand Down Expand Up @@ -104,9 +105,11 @@ private function registerDefaultServices($userSettings)
*
* @return EnvironmentInterface
*/
$this['environment'] = function ($c) {
return new Environment($_SERVER);
};
if (!isset($this['environment'])) {
$this['environment'] = function ($c) {
return new Environment($_SERVER);
};
}

/**
* PSR-7 Request object
Expand All @@ -115,9 +118,11 @@ private function registerDefaultServices($userSettings)
*
* @return ServerRequestInterface
*/
$this['request'] = function ($c) {
return Request::createFromEnvironment($c['environment']);
};
if (!isset($this['request'])) {
$this['request'] = function ($c) {
return Request::createFromEnvironment($c['environment']);
};
}

/**
* PSR-7 Response object
Expand All @@ -126,12 +131,14 @@ private function registerDefaultServices($userSettings)
*
* @return ResponseInterface
*/
$this['response'] = function ($c) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
if (!isset($this['response'])) {
$this['response'] = function ($c) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);

return $response->withProtocolVersion($c['settings']['httpVersion']);
};
return $response->withProtocolVersion($c['settings']['httpVersion']);
};
}

/**
* This service MUST return a SHARED instance
Expand All @@ -141,17 +148,19 @@ private function registerDefaultServices($userSettings)
*
* @return RouterInterface
*/
$this['router'] = function ($c) {
$router = new Router();
if (!isset($this['router'])) {
$this['router'] = function ($c) {
$router = new Router();

$uri = $c['request']->getUri();
$uri = $c['request']->getUri();

if (is_callable([$uri, 'getBasePath'])) {
$router->setBasePath($uri->getBasePath());
}
if (is_callable([$uri, 'getBasePath'])) {
$router->setBasePath($uri->getBasePath());
}

return $router;
};
return $router;
};
}

/**
* This service MUST return a SHARED instance
Expand All @@ -161,9 +170,11 @@ private function registerDefaultServices($userSettings)
*
* @return InvocationStrategyInterface
*/
$this['foundHandler'] = function ($c) {
return new RequestResponse();
};
if (!isset($this['foundHandler'])) {
$this['foundHandler'] = function ($c) {
return new RequestResponse();
};
}

/**
* This service MUST return a callable
Expand All @@ -180,9 +191,11 @@ private function registerDefaultServices($userSettings)
*
* @return callable
*/
$this['errorHandler'] = function ($c) {
return new Error();
};
if (!isset($this['errorHandler'])) {
$this['errorHandler'] = function ($c) {
return new Error();
};
}

/**
* This service MUST return a callable
Expand All @@ -198,9 +211,11 @@ private function registerDefaultServices($userSettings)
*
* @return callable
*/
$this['notFoundHandler'] = function ($c) {
return new NotFound();
};
if (!isset($this['notFoundHandler'])) {
$this['notFoundHandler'] = function ($c) {
return new NotFound();
};
}

/**
* This service MUST return a callable
Expand All @@ -217,9 +232,11 @@ private function registerDefaultServices($userSettings)
*
* @return callable
*/
$this['notAllowedHandler'] = function ($c) {
return new NotAllowed;
};
if (!isset($this['notAllowedHandler'])) {
$this['notAllowedHandler'] = function ($c) {
return new NotAllowed;
};
}

/**
* Instance of \Slim\Interfaces\CallableResolverInterface
Expand All @@ -228,9 +245,11 @@ private function registerDefaultServices($userSettings)
*
* @return CallableResolverInterface
*/
$this['callableResolver'] = function ($c) {
return new CallableResolver($c);
};
if (!isset($this['callableResolver'])) {
$this['callableResolver'] = function ($c) {
return new CallableResolver($c);
};
}
}

/********************************************************************************
Expand Down