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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"scripts": {
"check-code": "vendor/bin/phpcs && vendor/bin/phpstan",
"phpmd": "vendor/bin/phpmd --ignore-violations-on-exit --ignore-errors-on-exit src text phpmd.xml" ,
"phpstan": "vendor/bin/phpstan",
"phpcbf": "vendor/bin/phpcbf",
"churn": "vendor/bin/churn",
Expand Down
30 changes: 23 additions & 7 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;
use Phparch\SpaceTraders;
use Phparch\SpaceTraders\Routes;
use Phparch\SpaceTraders\ServiceContainer;
use Phparch\SpaceTraders\TwigExtensions;
use Symfony\Component\Cache\Adapter\RedisAdapter;

return [
Predis\Client::class => function () {
Predis\Client::class => static function () {
return new Predis\Client($_ENV['REDIS_URI']);
},
GuzzleHttp\Client::class => function () {
GuzzleHttp\Client::class => static function () {
$adapter = new RedisAdapter(
redis: ServiceContainer::get(Predis\Client::class),
namespace: '',
Expand All @@ -27,20 +28,35 @@
$stack->push(new CacheMiddleware($strategy), 'cache');
return new GuzzleHttp\Client(['handler' => $stack]);
},
SpaceTraders\RoutesMapper::class => function () {
return new SpaceTraders\RoutesMapper(
srcRootDir: dirname(__DIR__) . '/src/',
Routes\Scanner::class => static function () {
return new Routes\Scanner(
controllerDirs: [
[
'namespace' => 'Phparch\\SpaceTraders',
'path' => dirname(__DIR__) . '/src/Controller/'
]
],
ref: ServiceContainer::get(
\Roave\BetterReflection\BetterReflection::class
),
useAPCu: $_ENV['USE_APCU'] === 1,
);
},
Routes\Mapper::class => static function () {
return new SpaceTraders\Routes\Mapper(
scanner: ServiceContainer::get(Routes\Scanner::class),
registry: ServiceContainer::get(Routes\Registry::class),
);
},
Routes\Registry::class => static function () {
return new Routes\Registry(
container: ServiceContainer::instance(),
useAPCu: $_ENV['USE_APCU'] === 1
router: ServiceContainer::get(League\Route\Router::class),
decorator: ServiceContainer::get(Routes\Decorator::class)
);

},
Twig\Environment::class => function () {
Twig\Environment::class => static function () {
$twig = new Twig\Environment(
new \Twig\Loader\FilesystemLoader(dirname(__DIR__) . '/templates/'),
[
Expand Down
1 change: 1 addition & 0 deletions spacetraders
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $dotenv->load();

$services = require __DIR__ . '/config/services.php';

ServiceContainer::setEnv($_ENV);
ServiceContainer::config($services);
// Register dynamic services
ServiceContainer::autodiscover();
Expand Down
7 changes: 3 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Phparch\SpaceTraders;

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -56,11 +55,11 @@ public function getAllPages(
}

$keepFetching = true;
$i = 0;
$iter = 0;
$data = [];
$max = null;
$page = 1;
while ($keepFetching && $i < 100) {
while ($keepFetching && $iter < 100) {
// "rebuild" the params
$parts = parse_url($url);

Expand Down Expand Up @@ -98,7 +97,7 @@ public function getAllPages(
$keepFetching = false;
}

$i++;
$iter++;
$page++;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ContractsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
* @throws BadRequestException
*/
#[Route(name: 'accept_contract', path: '/contract/accept', methods: ['POST'])]
public function AccceptContract(): array
public function accceptContract(): array
{
/**
* @var array{
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/FleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ public function extractShip(): array
)]
public function shipInfo(): ResponseInterface
{
$ID = $this->getShipID();
$ship = $this->client->getShip($ID);
$shipID = $this->getShipID();
$ship = $this->client->getShip($shipID);

$atFuelStation = (
$ship->nav->route->destination->isFuelStation()
&& !$ship->nav->isInTransit()
);

$wp = $this->systems->systemLocation(
$waypoint = $this->systems->systemLocation(
system: $ship->nav->route->destination->systemSymbol,
waypoint: $ship->nav->route->destination->symbol
);
Expand All @@ -270,7 +270,7 @@ public function shipInfo(): ResponseInterface
'ship' => $ship,
'flightModes' => $flightModes,
'atFuelStation' => $atFuelStation,
'atMarket' => $wp->hasMarket(),
'atMarket' => $waypoint->hasMarket(),
]);
}

Expand Down
43 changes: 43 additions & 0 deletions src/Routes/Decorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Phparch\SpaceTraders\Routes;

use GuzzleHttp\Psr7\HttpFactory;
use InvalidArgumentException;
use League\Route\Route;
use League\Route\Strategy\ApplicationStrategy;
use League\Route\Strategy\JsonStrategy;
use Phparch\SpaceTraders\RouteInfo;
use Phparch\SpaceTraders\TwigAwareInterface;
use Twig\Environment;

class Decorator
{
public function applyTwigEnvironment(
object $controller,
Environment $twig,
): void {
if ($controller instanceof TwigAwareInterface) {
$controller->setTwigEnvironment($twig);
}
}

public function applyStrategy(Route $route, RouteInfo $info): void
{
if ($info->strategy) {
switch ($info->strategy) {
case 'application':
$route->setStrategy(new ApplicationStrategy());
break;
case 'json':
$responseFactory = new HttpFactory();
$route->setStrategy(new JsonStrategy($responseFactory));
break;
default:
throw new InvalidArgumentException(
"Unknown route response strategy"
);
}
}
}
}
34 changes: 34 additions & 0 deletions src/Routes/Mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Phparch\SpaceTraders\Routes;

use DI\DependencyException;
use DI\NotFoundException;
use League\Route\Router;

/**
* Registers discovered paths in the router
*/
class Mapper
{
public function __construct(
private Scanner $scanner,
private Registry $registry,
) {
}

/**
* @throws DependencyException
* @throws NotFoundException
*/
public function registerAll(): Router
{
$discovered = $this->scanner->discoverRoutes();

foreach ($discovered as $routeInfo) {
$this->registry->registerOne($routeInfo);
}

return $this->registry->getRouter();
}
}
66 changes: 66 additions & 0 deletions src/Routes/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Phparch\SpaceTraders\Routes;

use DI\Container;
use GuzzleHttp\Psr7\HttpFactory;
use http\Exception\InvalidArgumentException;
use League\Route\Router;
use League\Route\Strategy\ApplicationStrategy;
use League\Route\Strategy\JsonStrategy;
use Phparch\SpaceTraders\RequestAwareInterface;
use Phparch\SpaceTraders\RouteInfo;
use Phparch\SpaceTraders\TwigAwareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Twig\Environment;

class Registry
{
public function __construct(
private Container $container,
public Router $router,
private Decorator $decorator
) {
}

public function getRouter(): Router
{
return $this->router;
}


/**
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
*/
public function registerOne(
RouteInfo $info
): void {
$controller = $this->container->get($info->class);

$twig = $this->container->get(Environment::class);

if (
$twig instanceof Environment
&& is_object($controller)
) {
$this->decorator->applyTwigEnvironment(
$controller,
$twig,
);
}

$route = $this->router->map(
$info->httpMethods,
$info->path,
function (ServerRequestInterface $request) use ($controller, $info): mixed {
if ($controller instanceof RequestAwareInterface) {
$controller->setRequest($request);
}
return $controller->{$info->method}();
}
);

$this->decorator->applyStrategy($route, $info);
}
}
Loading