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

Http-Factory #27

Closed
wants to merge 10 commits into from
Closed

Http-Factory #27

wants to merge 10 commits into from

Conversation

danopz
Copy link
Member

@danopz danopz commented Jun 20, 2017

I just tried to implement the PSR-17 interfaces for Slim-Http addressing #1. I copied some of the existing static methods, such as Request::createFromGlobals(). Maybe we could even call them inside the factories - else not we should remove them.
This is still not done as the classes needs some tests and there still is a todo in the ServerRequestFactory.
I just wanted to leave this here to get a review before I waste additional hours, and to start a discussion wheter we should implement them here or in an additional package.

  • Write some tests
  • call static methods or remove them
  • fix the todo in ServerRequestFactory
  • ...

@coveralls
Copy link

Coverage Status

Coverage decreased (-8.0%) to 90.241% when pulling f1e4d0e on copynpaste:psr17 into 7ad2514 on slimphp:master.

@akrabat
Copy link
Member

akrabat commented Jun 27, 2017

I'm in favour of supporting PSR-17

@coveralls
Copy link

Coverage Status

Coverage decreased (-18.0007%) to 80.211% when pulling ac8ec09 on danopz:psr17 into 960af6e on slimphp:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.7%) to 97.493% when pulling 2062d61 on danopz:psr17 into 960af6e on slimphp:master.

@danopz
Copy link
Member Author

danopz commented Jul 5, 2017

I pushed my last commits.

  • updated factories
  • moved Uri::createFromGlobals to UriFactory with @internal flag
  • removed static factories from implementations, where PSR factories exists
  • added http-interop/http-factory-tests for factory tests
  • moved some tests to factory tests

Don't know how we should handle UploadedFile::createFromGlobals since the PSR doesn't handle this case we are using right now - maybe we should add a middleware for this?
Also don't know why the test suddenly fails on PHP7+?

Note: I just renamed using a more appropriate name 😉

@danopz danopz changed the title [WIP] Http-Factory Http-Factory Jul 6, 2017
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.7%) to 97.493% when pulling d7f72d4 on danopz:psr17 into 960af6e on slimphp:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.7%) to 97.493% when pulling 47dd163 on danopz:psr17 into 37e80cc on slimphp:master.

@coveralls
Copy link

coveralls commented Oct 8, 2017

Coverage Status

Coverage decreased (-0.4%) to 97.391% when pulling a21eb30 on danopz:psr17 into 4c76c96 on slimphp:master.

@danopz
Copy link
Member Author

danopz commented Oct 8, 2017

Rebased to current master / fixed tests

@akrabat
Copy link
Member

akrabat commented Dec 23, 2017

This is great work, but I'm not prepared to put it in until PSR-17 is ratified and published in the psr namespace.

Let's keep this around as a WIP until then.

@akrabat akrabat changed the title Http-Factory Http-Factory (WIP - waiting for PSR-17) Dec 23, 2017
@MarcHagen
Copy link

Maybe starting a discussion but, is it a idea to separate the both? So Slim-Http, Slim-HttpFactory ?
Both can have there respected (dev) requirements and then can even be used seperately.
Let me know :)

@danopz
Copy link
Member Author

danopz commented Jul 30, 2018

After thinking about this I'm also in favor of creating a dedicated factory repository.
So they get their own dependencies and they will respect PHP versions 5/7 (PSR7 is PHP 5+, PSR17 is PHP 7+).

@danopz
Copy link
Member Author

danopz commented Jul 31, 2018

Updated to psr/http-factory 1.0.0.
Tests are failing because of an increased PHP version + #54

@danopz danopz changed the title Http-Factory (WIP - waiting for PSR-17) Http-Factory Aug 23, 2018
@danopz
Copy link
Member Author

danopz commented Aug 23, 2018

@akrabat should we create an extra package to keep dependencies clean?

@akrabat
Copy link
Member

akrabat commented Aug 24, 2018

Maybe we make Slim\Http a PHP 7+ library?

This was referenced Sep 7, 2018
@akrabat
Copy link
Member

akrabat commented Sep 16, 2018

This needs rebasing to pick up the changes that make Slim-Http PHP 7+

@danopz
Copy link
Member Author

danopz commented Sep 16, 2018

Rebased 👍

@ghost
Copy link

ghost commented Sep 22, 2018

I'm not in favour of splitting this to a separate package. It's all directly related, i.e. you can't use the factories without the messages.

@ghost
Copy link

ghost commented Sep 22, 2018

Also, can we not make a single class for RequestFactory and ServerRequestFactory implementing both interfaces? It doesn't need to be separate as it's final output is the same class; it makes sense to me to have a single factory for a single class type.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

In general as I've commented, I feel that Factory classes are injectable dependencies, not inline newables. We should follow that method IMO.

public function createRequest(string $method, $uri): RequestInterface
{
if (is_string($uri)) {
$uri = (new UriFactory())->createUri($uri);
Copy link

Choose a reason for hiding this comment

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

I'm not comfortable with this. A Factory class is an injectable dependency, not a newable class. Should this not be injected via a constructor?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I agree, so one could inject some other factory and must not live with ours.

throw new \InvalidArgumentException('URI must either be string or instance of ' . UriInterface::class);
}

$body = (new StreamFactory())->createStream();
Copy link

Choose a reason for hiding this comment

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

Again as with UriFactory.

public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
if (is_string($uri)) {
$uri = (new UriFactory())->createUri($uri);
Copy link

Choose a reason for hiding this comment

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

Again, an injectable dependency IMO.

throw new \InvalidArgumentException('URI must either be string or instance of ' . UriInterface::class);
}

$body = (new StreamFactory())->createStream();
Copy link

Choose a reason for hiding this comment

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

Again, an injectable dependency IMO.

throw new \InvalidArgumentException('URI cannot be parsed');
}

$scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
Copy link

Choose a reason for hiding this comment

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

If we're using PHP 7, could we do something like:

$scheme = $parts['scheme'] ?? '';

@akrabat
Copy link
Member

akrabat commented Sep 22, 2018

Also, can we not make a single class for RequestFactory and ServerRequestFactory implementing both interfaces? It doesn't need to be separate as it's final output is the same class; it makes sense to me to have a single factory for a single class type.

Should we have a Request and ServerRequest in Slim-Http?

…ct, using null coalescing operator for UriFactory
@danopz
Copy link
Member Author

danopz commented Sep 22, 2018

Now we can inject a StreamFactoryInterface and UriFactoryInterface in both RequestFactories defaulting to null and so to our factories.
I'm not sure about merging RequestFactory and ServerRequestFactory.

@akrabat
Copy link
Member

akrabat commented Oct 30, 2018

This needs to be against Slim-Psr7 now.

slimphp/Slim-Psr7#14 tracks it.

@akrabat akrabat closed this Oct 30, 2018
danopz added a commit to danopz/Slim-Psr7 that referenced this pull request Nov 1, 2018
@danopz danopz deleted the psr17 branch November 1, 2018 22:45
akrabat pushed a commit to danopz/Slim-Psr7 that referenced this pull request Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants