Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:
composer test-unit-coverage

- name: SonarCloud Scan
uses: SonarSource/sonarqube-scan-action@v5
uses: SonarSource/sonarqube-scan-action@v6.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Expand Down
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
.bash_history
.cache
.config
.local
.composer
.composer/
composer.lock
vendor/
.bash_history
tests/_output
.env
.local
storage/logs/
tests/_output
vendor/
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"pds/skeleton": "^1.0",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^11.5",
"squizlabs/php_codesniffer": "^3.13"
"squizlabs/php_codesniffer": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
66 changes: 21 additions & 45 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,41 @@

declare(strict_types=1);

use Phalcon\Api\Domain\Interfaces\DomainInterface;
use Phalcon\Api\Domain\Interfaces\ResponderInterface;
use Phalcon\Api\Domain\Middleware\ResponseSender;
use Phalcon\Api\Domain\Services\ActionHandler;
use Phalcon\Api\Domain\Services\Container;
use Phalcon\Api\Domain\Services\Providers\ErrorHandlerProvider;
use Phalcon\Api\Domain\Services\Providers\RouterProvider;
use Phalcon\Di\ServiceProviderInterface;
use Phalcon\Mvc\Micro;

require_once dirname(__DIR__) . '/vendor/autoload.php';

$container = new Container();
$application = new Micro($container);
$container->set(Container::APPLICATION, $application, true);
$now = hrtime(true);
$container->set(
Container::TIME,
function () use ($now) {
return $now;
},
true
);

/**
* Routes
* Providers
*/
$routes = [
[
'method' => 'get',
'pattern' => '/',
'service' => Container::HELLO_SERVICE,
'responder' => Container::HELLO_RESPONDER_JSON,
],
$providers = [
ErrorHandlerProvider::class,
RouterProvider::class,
];

foreach ($routes as $route) {
$method = $route['method'];
$pattern = $route['pattern'];
$serviceName = $route['service'];
$responderName = $route['responder'];

$application->$method(
$pattern,
function () use ($container, $serviceName, $responderName) {
/** @var DomainInterface $service */
$service = $container->get($serviceName);
/** @var ResponderInterface $responder */
$responder = $container->get($responderName);

$action = new ActionHandler($service, $responder);
$action->__invoke();
}
);
/** @var class-string $provider */
foreach ($providers as $provider) {
/** @var ServiceProviderInterface $service */
$service = new $provider();
$container->register($service);
}

$application->finish(
function () use ($container) {
$response = $container->getShared(Container::RESPONSE);
$sender = new ResponseSender();

$sender->__invoke($response);
}
);

$application->notFound(
function () {
echo "404 - Not Found - " . date("Y-m-d H:i:s");
}
);


/** @var string $uri */
$uri = $_SERVER['REQUEST_URI'] ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

declare(strict_types=1);

namespace Phalcon\Api\Domain\Services;
namespace Phalcon\Api\Domain\ADR\Action;

use Phalcon\Api\Domain\Interfaces\ActionInterface;
use Phalcon\Api\Domain\Interfaces\DomainInterface;
use Phalcon\Api\Domain\Interfaces\ResponderInterface;
use Phalcon\Api\Domain\ADR\Domain\DomainInterface;
use Phalcon\Api\Domain\ADR\Responder\ResponderInterface;

final readonly class ActionHandler implements ActionInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Phalcon\Api\Domain\Interfaces;
namespace Phalcon\Api\Domain\ADR\Action;

interface ActionInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Phalcon\Api\Domain\Interfaces;
namespace Phalcon\Api\Domain\ADR\Domain;

use Phalcon\Domain\Payload;

Expand Down
40 changes: 40 additions & 0 deletions src/Domain/ADR/Responder/JsonResponder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\ADR\Responder;

use Phalcon\Api\Domain\Services\Http\Response;
use Phalcon\Domain\Payload;

final class JsonResponder implements ResponderInterface
{
public function __construct(
private Response $response
) {
}

public function __invoke(Payload $payload): Response
{
$result = $payload->getResult();
/** @var string $content */
$content = $result['results'];

$this
->response
->withPayloadData([$content])
->render()
;

return $this->response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Phalcon\Api\Domain\Interfaces;
namespace Phalcon\Api\Domain\ADR\Responder;

use Phalcon\Domain\Payload;
use Phalcon\Http\ResponseInterface;
Expand Down
44 changes: 44 additions & 0 deletions src/Domain/Constants/Dates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\Constants;

use DateTimeImmutable;
use DateTimeZone;
use Exception;

final class Dates
{
public const DATE_FORMAT = 'Y-m-d';
public const DATE_NOW = 'NOW()';
public const DATE_TIME_FORMAT = 'Y-m-d H:i:s';
public const DATE_TIME_UTC_FORMAT = 'Y-m-d\\TH:i:sp';
public const DATE_TIME_ZONE = 'UTC';

/**
* @param string $date
* @param string $format
*
* @return string
* @throws Exception
*/
public static function toUTC(
string $date = 'now',
string $format = self::DATE_TIME_UTC_FORMAT
): string {
return (new DateTimeImmutable(
$date,
new DateTimeZone(self::DATE_TIME_ZONE)
))->format($format);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Phalcon\Api\Domain\Exceptions;

class InvalidConfigurationArgumentException extends \InvalidArgumentException
use InvalidArgumentException;

class InvalidConfigurationArgumentException extends InvalidArgumentException
{
}
5 changes: 3 additions & 2 deletions src/Domain/Hello/HelloService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
namespace Phalcon\Api\Domain\Hello;

use PayloadInterop\DomainStatus;
use Phalcon\Api\Domain\Interfaces\DomainInterface;
use Phalcon\Api\Domain\ADR\Domain\DomainInterface;
use Phalcon\Api\Domain\Constants\Dates;
use Phalcon\Domain\Payload;

use function date;
Expand All @@ -26,7 +27,7 @@ public function __invoke(): Payload
return new Payload(
DomainStatus::SUCCESS,
[
'results' => "Hello World!!! - " . date("Y-m-d H:i:s")
'results' => "Hello World!!! - " . date(Dates::DATE_TIME_FORMAT),
]
);
}
Expand Down
62 changes: 62 additions & 0 deletions src/Domain/Interfaces/RoutesInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\Interfaces;

use BackedEnum;

/**
* Interface for route enumerations
*
* @phpstan-type TMiddleware array<string, 'before'|'finish'>
*/
interface RoutesInterface extends BackedEnum
{
public const DELETE = 'delete';
public const EVENT_BEFORE = 'before';
public const EVENT_FINISH = 'finish';
public const GET = 'get';
public const POST = 'post';
public const PUT = 'put';


/**
* @return string
*/
public function endpoint(): string;

/**
* @return string
*/
public function method(): string;

/**
* @return TMiddleware
*/
public static function middleware(): array;

/**
* @return string
*/
public function prefix(): string;

/**
* @return string
*/
public function service(): string;

/**
* @return string
*/
public function suffix(): string;
}
Loading
Loading