Skip to content

Commit

Permalink
http-factory updates and cs fixes (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes authored Nov 3, 2023
1 parent 0ced97b commit 3a81f97
Show file tree
Hide file tree
Showing 33 changed files with 252 additions and 122 deletions.
7 changes: 5 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
->exclude('vendor')
;

$config = new PhpCsFixer\Config();
return $config->setRules([
return (new PhpCsFixer\Config())->setRules([
// Rule sets
'@PER-CS' => true,
'@PHP80Migration:risky' => true,
'@PHP81Migration' => true,
'@PHPUnit100Migration:risky' => true,

// Rules
'no_unused_imports' => true,
'php_unit_test_class_requires_covers' => true,
])->setFinder($finder);
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ lint-php: # lint php files
coverage: ## Build Code Coverage Report
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --coverage-html $(COVERAGE_DIR)

coverage-http-factory:
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --testsuite http-factory --coverage-html $(COVERAGE_DIR)

psalm: ## Run psalm
XDEBUG_MODE=off $(PHP) $(PSALM)

Expand Down
19 changes: 19 additions & 0 deletions docs/components/http-factory/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,22 @@ Simple PSR-17 Http Factory Component
```shell
composer require sonsofphp/http-factory
```

## Usage

```php
use SonsOfPHP\Component\HttpFactory\HttpFactory;

$factory = new HttpFactory();

$request = $factory->createRequest($method, $uri);
$response = $factory->createResponse();
$serverRequest = $factory->createServerRequest($method, $uri);
$stream = $factory->createStream();
$stream = $factory->createStreamFromFile('/path/to/file.ext');
$stream = $factory->createStreamFromResource($resource);
$uploadedFile = $factory->createUploadedFile($stream);
$uri = $factory->createUri('https://docs.sonsofphp.com');
```

For more details, please see the source code or review the PSR-17 documentation.
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="false">
beStrictAboutCoverageMetadata="true">

<testsuites>
<testsuite name="all">
Expand All @@ -17,6 +17,10 @@
<directory>src/SonsOfPHP/Bundle/*/Tests</directory>
<directory>src/SonsOfPHP/Component/*/Tests</directory>
</testsuite>

<testsuite name="http-factory">
<directory>src/SonsOfPHP/Component/HttpFactory/Tests</directory>
</testsuite>
</testsuites>

<coverage/>
Expand Down
1 change: 0 additions & 1 deletion src/SonsOfPHP/Bard/src/Console/Command/CopyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SonsOfPHP\Bard\JsonFile;
use Symfony\Component\Console\Input\InputArgument;
use SonsOfPHP\Component\Json\Json;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;

/**
* Chain adapter allows you to use multiple adapters together.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;

/**
* If an adapter is able to manage directories, it should implement this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToReadFileException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;

/**
* The native adapter will use the underlying filesystem to store files.
Expand Down
1 change: 0 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SonsOfPHP\Component\Filesystem\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use SonsOfPHP\Component\Filesystem\Exception\UnableToReadFileException;
Expand Down
1 change: 0 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject;
use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Context;

Expand Down
2 changes: 0 additions & 2 deletions src/SonsOfPHP/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace SonsOfPHP\Component\Filesystem\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject;
use SonsOfPHP\Component\Filesystem\FilesystemInterface;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Filesystem;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;

Expand Down
22 changes: 22 additions & 0 deletions src/SonsOfPHP/Component/HttpFactory/HttpFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\RequestFactoryInterface;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
final class RequestFactory implements RequestFactoryInterface
{
use RequestFactoryTrait;
use ResponseFactoryTrait;
use ServerResponseFactoryTrait;
use StreamFactoryTrait;
use UploadedFileFactoryTrait;
use UriFactoryTrait;
}
14 changes: 2 additions & 12 deletions src/SonsOfPHP/Component/HttpFactory/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@
namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use SonsOfPHP\Component\HttpMessage\Request;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
class RequestFactory implements RequestFactoryInterface
final class RequestFactory implements RequestFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createRequest(string $method, $uri): RequestInterface
{
return new Request($method, $uri);
}
use RequestFactoryTrait;
}
24 changes: 24 additions & 0 deletions src/SonsOfPHP/Component/HttpFactory/RequestFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\RequestInterface;
use SonsOfPHP\Component\HttpMessage\Request;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
trait RequestFactoryTrait
{
/**
* {@inheritdoc}
*/
public function createRequest(string $method, $uri): RequestInterface
{
return new Request($method, $uri);
}
}
12 changes: 2 additions & 10 deletions src/SonsOfPHP/Component/HttpFactory/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@
namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use SonsOfPHP\Component\HttpMessage\Response;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
class ResponseFactory implements ResponseFactoryInterface
final class ResponseFactory implements ResponseFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return (new Response())->withStatus($code, $reasonPhrase);
}
use ResponseFactoryTrait;
}
24 changes: 24 additions & 0 deletions src/SonsOfPHP/Component/HttpFactory/ResponseFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\ResponseInterface;
use SonsOfPHP\Component\HttpMessage\Response;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
trait ResponseFactoryTrait
{
/**
* {@inheritdoc}
*/
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return (new Response())->withStatus($code, $reasonPhrase);
}
}
14 changes: 2 additions & 12 deletions src/SonsOfPHP/Component/HttpFactory/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@
namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use SonsOfPHP\Component\HttpMessage\ServerRequest;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
class ServerRequestFactory implements ServerRequestFactoryInterface
final class ServerRequestFactory implements ServerRequestFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return new ServerRequest($method, $uri, $serverParams);
}
use ServerRequestFactoryTrait;
}
24 changes: 24 additions & 0 deletions src/SonsOfPHP/Component/HttpFactory/ServerRequestFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\ServerRequestInterface;
use SonsOfPHP\Component\HttpMessage\ServerRequest;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
trait ServerRequestFactoryTrait
{
/**
* {@inheritdoc}
*/
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return new ServerRequest($method, $uri, $serverParams);
}
}
38 changes: 2 additions & 36 deletions src/SonsOfPHP/Component/HttpFactory/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,13 @@
namespace SonsOfPHP\Component\HttpFactory;

use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use SonsOfPHP\Component\HttpMessage\Stream;

/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
class StreamFactory implements StreamFactoryInterface
final class StreamFactory implements StreamFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createStream(string $content = ''): StreamInterface
{
$resource = fopen('php://temp', 'r+');
fwrite($resource, $content);
fseek($resource, 0);

return new Stream($resource);
}

/**
* {@inheritdoc}
*/
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
// #todo Error Handling, filename might be invalid or mode may be invalid

return new Stream(fopen($filename, $mode));
}

/**
* {@inheritdoc}
*/
public function createStreamFromResource($resource): StreamInterface
{
if (!is_resource($resource)) {
throw new \InvalidArgumentException('resource is invalid');
}

return new Stream($resource);
}
use StreamFactoryTrait;
}
Loading

0 comments on commit 3a81f97

Please sign in to comment.