{"action":"created","issue":{"url":"https://api.github.com/repos/relayphp/Relay.Relay/issues/18","labels_url":"https://api.github.com/repos/relayphp/Relay.Relay/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/relayphp/Relay.Relay/issues/18/comments","events_url":"https://api.github.com/repos/relayphp/Relay.Relay/issues/18/events","html_url":"https://github.com/relayphp/Relay.Relay/issues/18","id":114439174,"number":18,"title":"No access to real request object in middleware on the response's way out","user":{"login":"KorvinSzanto","id":1007419,"avatar_url":"https://avatars.githubusercontent.com/u/1007419?v=3","gravatar_id":"","url":"https://api.github.com/users/KorvinSzanto","html_url":"https://github.com/KorvinSzanto","followers_url":"https://api.github.com/users/KorvinSzanto/followers","following_url":"https://api.github.com/users/KorvinSzanto/following{/other_user}","gists_url":"https://api.github.com/users/KorvinSzanto/gists{/gist_id}","starred_url":"https://api.github.com/users/KorvinSzanto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KorvinSzanto/subscriptions","organizations_url":"https://api.github.com/users/KorvinSzanto/orgs","repos_url":"https://api.github.com/users/KorvinSzanto/repos","events_url":"https://api.github.com/users/KorvinSzanto/events{/privacy}","received_events_url":"https://api.github.com/users/KorvinSzanto/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-10-31T22:56:07Z","updated_at":"2015-11-02T20:33:23Z","closed_at":null,"body":"Since `->withAttribute` and `->getAttribute` only exist on `\\Psr\\Http\\Message\\ServerRequestInterface` which is immutable, important things that need to be passed through by a middleware might not be available on the way out. \r\nAn example of this being an issue is maybe a cookie encrypting middleware:\r\n\r\n```php\r\nclass CookieEncryptor\r\n{\r\n\r\n    use CookieEncryptingTrait, CookieDecryptingTrait, CookieSerializingTrait;\r\n\r\n    public function __invoke($request, $response)\r\n    {\r\n        // Before the router\r\n        $cookies = $this->decryptedCookies($request);\r\n        $request = $request->withAttribute('cookies', $cookies);\r\n\r\n        $response = $next($request, $response);\r\n        // After the router\r\n        // $request and $cookies may have been replaced, but we have no way to know.\r\n        return $response->withHeader('Set-cookie', $this->serializeCookies($this->encryptedCookies($cookies->getModified())));\r\n    }\r\n\r\n}\r\n```\r\n\r\nOne way to fix this is to append a reversed version of the queue onto the queue before sending a request and response, but then the middleware has to maintain state and the recommended way of catching the response on the way out becomes confusing.\r\n\r\nThoughts?\r\n"},"comment":{"url":"https://api.github.com/repos/relayphp/Relay.Relay/issues/comments/153148371","html_url":"https://github.com/relayphp/Relay.Relay/issues/18#issuecomment-153148371","issue_url":"https://api.github.com/repos/relayphp/Relay.Relay/issues/18","id":153148371,"user":{"login":"weierophinney","id":25943,"avatar_url":"https://avatars.githubusercontent.com/u/25943?v=3","gravatar_id":"","url":"https://api.github.com/users/weierophinney","html_url":"https://github.com/weierophinney","followers_url":"https://api.github.com/users/weierophinney/followers","following_url":"https://api.github.com/users/weierophinney/following{/other_user}","gists_url":"https://api.github.com/users/weierophinney/gists{/gist_id}","starred_url":"https://api.github.com/users/weierophinney/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/weierophinney/subscriptions","organizations_url":"https://api.github.com/users/weierophinney/orgs","repos_url":"https://api.github.com/users/weierophinney/repos","events_url":"https://api.github.com/users/weierophinney/events{/privacy}","received_events_url":"https://api.github.com/users/weierophinney/received_events","type":"User","site_admin":false},"created_at":"2015-11-02T20:33:23Z","updated_at":"2015-11-02T20:33:23Z","body":"@KorvinSzanto — I just remembered another approach that @pmjones and I discussed: having a stateful request attribute (an object).\r\n\r\n```php\r\n// child middleware\r\n$request->getAttribute('accumulator')->test = true;\r\n\r\n// Parent middleware:\r\n$accumulator = (object) ['test' => false];\r\n$request = $request->withAttribute('accumulator', $accumulator);\r\n\r\n$response = $next($request, $response);\r\nif ($request->getAttribute('accumulator')->test) {\r\n    // Changed!\r\n}\r\n```\r\n\r\nIt kind of flies in the face of immutability, but we specifically chose not to stipulate that all attribute values were immutable. As such, it's a valid way to pass around state if necessary, and, I'd argue, safer than accepting a changed request instance from the child."}}