Heya,
Sorry, it is indeed true that I told you what not to do, without telling you why it is like that
| jobsfan
June 8 |
It affect the performance? I mean it will slow down the response speed of the web services?
Or it will make concurrent smaller?
The reason you want to avoid having Container
(or ServiceManager
, or any similar service locator structure) in your services is that your service should:
-
be as-stateless-as-possible: this means that the service does not mutate during runtime, so that multiple usages of it lead to reproducible results.
-
be only aware of its direct dependencies: your AuthManager
is only responsible for handling authentication, so it should not mess with other unrelated services, nor should it modify them
-
the ServiceManager
itself is also a service, and should stay immutable post-initialization
I tried to print_r the var $container, it is very big. The point is that I should not pass a very big var in between the functions of my application?
What I am trying to solve is that:
I need to get the authorization via cookie, header[authentication line], or get param($_[‘GET’]), I do not need a normal login or logout logic in my application, this part jobs done by other subdomain.
It sounds like your authorization service needs a Request
object as input, so it can take the decision (using a header, the session cookie, or a query string parameter) to take runtime decisions.
I want to centeral controller the authentication and store it in a shared servie, maybe just a global shared var. And then I can fetch it from any where like controller plugin, view helper, etc.
The “authentication” being the current “context” an user is operating within, right? It should probably always be queried as:
$authentication = $this->authManager->contextFor($request);
// now operate with $authentication
Alternatively, if you are operating with a PSR-7/PSR-15 structure, you can have this done by a middleware that injects the authentication (see presentation @ https://ocramius.github.io/from-helpers-to-middleware/#/69 )
final class InitializeSession implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
{
return $delegate->process(
$request->withAttribute(
‘session’,
$this->loadSession($request)
)
);
}
private function loadSession(ServerRequestInterface $request) : array
{
// ...
}
}
If I don’t change the $container as you said. What I can think about is to do this logic at the action. Then I need to write many duplicate code in many actions.
Yes, duplication is still preferable to hidden global state mutation though.
Marco Pivetta
http://twitter.com/Ocramius
http://ocramius.github.com/