Skip to content

Commit

Permalink
Getting start for 7
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Feb 10, 2021
1 parent c9bd303 commit 54dd5e8
Show file tree
Hide file tree
Showing 28 changed files with 243 additions and 205 deletions.
211 changes: 48 additions & 163 deletions .buildpath

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php
php:
- '7.4'
- '8.0'

git:
depth: 1
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
],
"autoload" : {
"psr-4" : {
"Pluf\\" : "src/"
"Pluf\\Core\\" : "src/"
}
},
"autoload-dev" : {
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Pluf;
namespace Pluf\Core;

use Throwable;
use JsonSerializable;
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/InvalidBodyContentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Pluf\Core\Exception;

class InvalidBodyContentException extends \Pluf\Core\Exception
{
}

14 changes: 14 additions & 0 deletions src/Exception/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Pluf\Core\Exception;

use Throwable;

class ResourceNotFoundException extends \Pluf\Core\Exception
{

public function __construct($message = '', ?int $code = null, ?Throwable $previous = null, ?int $status = 404, ?array $params = [], ?array $solutions = [])
{
parent::__construct($message, $code, $previous, $status, $params, $solutions);
}
}

14 changes: 14 additions & 0 deletions src/Exception/UnhandledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Pluf\Core\Exception;

use Throwable;

class UnhandledException extends \Pluf\Core\Exception
{

public function __construct($message = '', ?int $code = null, ?Throwable $previous = null, ?int $status = 500, ?array $params = [], ?array $solutions = [])
{
parent::__construct($message, $code, $previous, $status, $params, $solutions);
}
}

2 changes: 1 addition & 1 deletion src/ExceptionBuilder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Pluf;
namespace Pluf\Core;

use Throwable;

Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/AccessLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\RequestInterface;
Expand Down
7 changes: 0 additions & 7 deletions src/Process/Http/Exception/InvalidBodyContentException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Process/Http/FileToResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfMethodIs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\RequestInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfMethodIsDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

class IfMethodIsDelete extends IfMethodIs
{
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfMethodIsGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

class IfMethodIsGet extends IfMethodIs
{
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfMethodIsPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

class IfMethodIsPost extends IfMethodIs
{
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfMethodIsPut.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

class IfMethodIsPut extends IfMethodIs
{
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfPathAndMethodIs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\RequestInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Http/IfPathIs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\RequestInterface;
Expand Down
4 changes: 1 addition & 3 deletions src/Process/Http/RequestBodyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Pluf\Scion\Process\Http\Exception\InvalidBodyContentException;
Expand Down Expand Up @@ -81,8 +81,6 @@ public function __invoke(ServerRequestInterface $request, UnitTrackerInterface $
throw new InvalidBodyContentException("Not supported content type: " . $parsedContentType);
}
}

var_dump($request->getParsedBody());
}
// run pipeline
return $unitTracker->next([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Psr\Http\Message\ServerRequestInterface;
use Pluf\Core\Exception\ResourceNotFoundException;

/**
* Throw exception and break to path
Expand All @@ -25,12 +28,16 @@
* @author maso ([email protected])
*
*/
class ResourceNotFoundException
class ResourceNotFound
{

public function __invoke()
public function __invoke(ServerRequestInterface $request)
{
throw new \Exception("Resouce not found");
throw new ResourceNotFoundException("Requested resouce not found : {{resource}}",
status: 404,
params:[
"resource" => $request?->getUri()?->getPath()
]);
}
}

27 changes: 22 additions & 5 deletions src/Process/Http/ResponseBodyEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,50 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Pluf\Process\Http;
namespace Pluf\Core\Process\Http;

use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Log\LoggerInterface;
use Pluf\Core\Exception;

class ResponseBodyEncoder
{

//
//
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, StreamFactoryInterface $streamFactory, LoggerInterface $logger, UnitTrackerInterface $unitTracker)
{
$result = "";
$status = 200;
try {
$result = $unitTracker->next();
} catch (Throwable $t) {
$response = $response->withStatus(500);
if (! ($t instanceof Exception)) {
$ex = new Exception\UnhandledException(
message: $t->getMessage(),
previous: $t);
} else {
$ex = $t;
}

$status = $ex->getStatus();
// TODO: maso, 2021: enable exception json annotations
$result = [
'message' => $t->getMessage()
];
}


// TODO: maso, 2021: support objectMapper
// $supportMime = $request->getHeader("Accepted");
$resultEncode = json_encode($result);
return $response->withBody($streamFactory->createStream($resultEncode));
$contentType = 'application/json';

return $response->withStatus($status)
->withHeader("Content-Type", $contentType)
->withBody($streamFactory->createStream($resultEncode));
}
}

2 changes: 1 addition & 1 deletion tests/Process/Http/AccessLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

use PHPUnit\Framework\TestCase;
use Pluf\Scion\UnitTrackerInterface;
use Pluf\Scion\Process\Http\AccessLog;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Pluf\Core\Process\Http\AccessLog;

/**
* AccessLog test case.
Expand Down
2 changes: 1 addition & 1 deletion tests/Process/Http/FileToResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
namespace Pluf\Tests\Process\Http;

use PHPUnit\Framework\TestCase;
use Pluf\Core\Process\Http\FileToResponse;
use Pluf\Http\ResponseFactory;
use Pluf\Http\ServerRequestFactory;
use Pluf\Scion\UnitTrackerInterface;
use Pluf\Scion\Process\Http\FileToResponse;

class FileToResponseTest extends TestCase
{
Expand Down
11 changes: 6 additions & 5 deletions tests/Process/Http/IfMethodIsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

use PHPUnit\Framework\TestCase;
use Pluf\Scion\UnitTrackerInterface;
use Pluf\Scion\Process\Http\IfMethodIsDelete;
use Pluf\Scion\Process\Http\IfMethodIsGet;
use Pluf\Scion\Process\Http\IfMethodIsPost;
use Psr\Http\Message\RequestInterface;
use Pluf\Scion\Process\Http\IfMethodIsPut;
use Pluf\Scion\Process\Http\IfMethodIs;

use Pluf\Core\Process\Http\IfMethodIsDelete;
use Pluf\Core\Process\Http\IfMethodIsGet;
use Pluf\Core\Process\Http\IfMethodIsPost;
use Pluf\Core\Process\Http\IfMethodIsPut;
use Pluf\Core\Process\Http\IfMethodIs;

/**
* IfMethodIsDelete test case.
Expand Down
2 changes: 1 addition & 1 deletion tests/Process/Http/IfPathAndMethodIsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use PHPUnit\Framework\TestCase;
use Pluf\Http\UriFactory;
use Pluf\Scion\UnitTrackerInterface;
use Pluf\Scion\Process\Http\IfPathAndMethodIs;
use Pluf\Core\Process\Http\IfPathAndMethodIs;
use Psr\Http\Message\RequestInterface;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Process/Http/IfPathIsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use PHPUnit\Framework\TestCase;
use Pluf\Http\UriFactory;
use Pluf\Scion\UnitTrackerInterface;
use Pluf\Scion\Process\Http\IfPathIs;
use Pluf\Core\Process\Http\IfPathIs;
use Psr\Http\Message\RequestInterface;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Process/Http/RequestBodyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Pluf\Http\StreamFactory;
use Pluf\Http\UriFactory;
use Pluf\Scion\UnitTracker;
use Pluf\Scion\Process\Http\RequestBodyParser;
use Pluf\Core\Process\Http\RequestBodyParser;
use Pluf\Tests\Process\Http\Mock\ReturnRequestParsedBody;

class RequestBodyParserTest extends TestCase
Expand Down
Loading

0 comments on commit 54dd5e8

Please sign in to comment.