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

Pass settings to Pimple #1457

merged 1 commit into from
Aug 20, 2015

Conversation

akrabat
Copy link
Member

@akrabat akrabat commented Aug 20, 2015

This allows a user to configure the container via settings.

Fixes #1407

This allows a user to configure the container via settings.

Fixes #1407
@geggleto
Copy link
Member

You are a rockstar :) 👍

@codeguy
Copy link
Member

codeguy commented Aug 20, 2015

👍

{
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.

silentworks added a commit that referenced this pull request Aug 20, 2015
@silentworks silentworks merged commit 9430d0e into slimphp:3.x Aug 20, 2015
@silentworks silentworks added this to the 3.0.0 RC1 milestone Aug 20, 2015
@ConstantineYurevich
Copy link
Contributor

Unfortunately, this PR doesn't have backward compatibility. Before I was able to "override" container values before $app->run().

Before I was able to do the following in unit tests:

$app = new \Slim\App();

Configurator::configureApp($app); //seudo-code

$c = $app->getContainer();
$c['environment'] = $c->factory(function ($c) {
....
});
$c['request'] = $c->factory(function ($c) {
....
});

Now it is not possible, getting following error:

RuntimeException : Cannot override frozen service "environment".

And main problem is that now you freeze almost all container variables during router initialisation:

Before it was (worked perfectly):

 $this['router'] = function ($c) {
     return new Router();
};

Not it is:

if (!isset($this['router'])) {
            $this['router'] = function ($c) {
                $router = new Router();

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

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

                return $router;
            };
}

As you can see, now you access $c['request'], which automatically freezes almost all essential components in container.

@ConstantineYurevich
Copy link
Contributor

Main idea is that essential container components, such as environment, request and response should not be frozen before app run.

Use case:

  1. I want to configure my application (including all routes, acl, domain services, etc)
  2. Save it to static property or to cache.
  3. Override container's environment, request and response with factories.
  4. Run many tests against the same instance of application.

@akrabat
Copy link
Member Author

akrabat commented Aug 26, 2015

@yurevichcv Please open a new issue as it may be related to #1443 rather than this PR.

@silentworks
Copy link
Member

This has been the case before this change was made, you probably just didn't notice it before. Services are frozen from the minute they get called, this is how Pimple works.

@ConstantineYurevich
Copy link
Contributor

Yes, sorry, you are right. This PR caused the problem - #1443

@ConstantineYurevich
Copy link
Contributor

@silentworks Yes, I know how Pimple works. But environment, request and response should not be frozen before app run. This limits flexibility in testing. request is the most essential entry point, and it should be called only once, further it should just be "travelling" between middleware and routes.

The same issue was in Twig-View extension, where I also sumbmitted my PR against request freezing during initialization phase - slimphp/Twig-View#30

@geggleto
Copy link
Member

@yurevichcv you are free to overwrite the defaults at any time during execution by simply overwriting the default container instance like this.

unset($container['environment']);
$container['environment'] = new Environment();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants