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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Command to make middlewares. ([#19](https://github.com/barbosa89/phenix/pull/19))
- SonarCloud integration. ([#13](https://github.com/barbosa89/phenix/pull/13))
- PHPInsights integration. ([#12](https://github.com/barbosa89/phenix/pull/12))
- PHPStan integration. ([#11](https://github.com/barbosa89/phenix/pull/11))
Expand Down
29 changes: 29 additions & 0 deletions app/Http/Middleware/AcceptJsonResponses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Amp\Http\Server\Middleware;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Status;

class AcceptJsonResponses implements Middleware
{
public function handleRequest(Request $request, RequestHandler $next): Response
{
if ($this->acceptHtml($request)) {
return response()->json([], Status::NOT_ACCEPTABLE);
}

return $next->handleRequest($request);
}

private function acceptHtml(Request $request): bool
{
return $request->hasHeader('Accept')
&& str_contains($request->getHeader('Accept'), 'html');
}
}
17 changes: 15 additions & 2 deletions core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class App implements AppContract, Makeable

private Logger $logger;
private SocketHttpServer $server;
private bool $signalTrapping = true;
private DefaultErrorHandler $errorHandler;

public function __construct(string $path)
Expand Down Expand Up @@ -64,10 +65,17 @@ public function run(): void
{
$this->server->start(self::$container->get('router')->getRouter(), $this->errorHandler);

$signal = \Amp\trapSignal([SIGINT, SIGTERM]);
if ($this->signalTrapping) {
$signal = \Amp\trapSignal([SIGINT, SIGTERM]);

$this->logger->info("Caught signal {$signal}, stopping server");
$this->logger->info("Caught signal {$signal}, stopping server");

$this->stop();
}
}

public function stop(): void
{
$this->server->stop();
}

Expand All @@ -86,6 +94,11 @@ public function swap(string $key, object $concrete): void
self::$container->extend($key)->setConcrete($concrete);
}

public function disableSignalTrapping(): void
{
$this->signalTrapping = false;
}

private function setupDefinitions(): void
{
$this->registerFacades();
Expand Down
2 changes: 1 addition & 1 deletion core/AppBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function build(): AppContract
private static function loadRoutes(): void
{
foreach (Directory::all(base_path('routes')) as $file) {
require_once $file;
require $file;
}
}
}
10 changes: 10 additions & 0 deletions core/AppProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ public function run(): void
$this->app->run();
}

public function stop(): void
{
$this->app->stop();
}

public function swap(string $key, object $concrete): void
{
$this->app->swap($key, $concrete);
}

public function enableTestingMode(): void
{
$this->app->disableSignalTrapping();
}
}
50 changes: 50 additions & 0 deletions core/Console/Commands/MakeMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Core\Console\Commands;

use Core\Console\Maker;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeMiddleware extends Maker
{
/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultName = 'make:middleware';

/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultDescription = 'Creates a new middleware.';

protected function configure(): void
{
$this->setHelp('This command allows you to create a new middleware.');

$this->addArgument('name', InputArgument::REQUIRED, 'The middleware name');

$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to create middleware');
}

protected function outputDirectory(): string
{
return 'app/Http/Middleware';
}

protected function stub(): string
{
return 'middleware.stub';
}

protected function suffix(): string
{
return 'Middleware';
}
}
13 changes: 10 additions & 3 deletions core/Console/Commands/MakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@ protected function configure(): void

$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to create test');
$this->addOption('unit', 'u', InputOption::VALUE_NONE, 'Create unit testing');
$this->addOption('core', 'c', InputOption::VALUE_NONE, 'Create core testing');
}

protected function outputDirectory(): string
{
$base = 'tests' . DIRECTORY_SEPARATOR;

return $this->input->getOption('unit')
? $base . 'Unit'
: $base . 'Feature';
if ($this->input->getOption('unit')) {
return $base . 'Unit';
}

if ($this->input->getOption('core')) {
return $base . 'Core';
}

return $base . 'Feature';
}

protected function stub(): string
Expand Down
17 changes: 12 additions & 5 deletions core/Console/Maker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$name = $this->input->getArgument('name');
$force = $this->input->getOption('force');

$namespace = explode('/', $name);
$namespace = explode(DIRECTORY_SEPARATOR, $name);
$className = array_pop($namespace);

$filePath = $this->preparePath($namespace) . "/{$className}.php";
Expand Down Expand Up @@ -59,17 +59,24 @@ private function preparePath(array $namespace): string
{
$path = base_path($this->outputDirectory());

$this->checkDirectory($path);

foreach ($namespace as $directory) {
$path .= '/' . ucfirst($directory);
$path .= DIRECTORY_SEPARATOR . ucfirst($directory);

if (! File::exists($path)) {
File::createDirectory($path);
}
$this->checkDirectory($path);
}

return $path;
}

private function checkDirectory(string $path): void
{
if (! File::exists($path)) {
File::createDirectory($path);
}
}

/**
* @param array<int, string> $namespace
*/
Expand Down
2 changes: 2 additions & 0 deletions core/Contracts/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ interface App
{
public function run(): void;

public function stop(): void;

public function swap(string $key, object $concrete): void;
}
2 changes: 2 additions & 0 deletions core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
if (! function_exists('base_path()')) {
function base_path(string $path = ''): string
{
$path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);

return App::path() . DIRECTORY_SEPARATOR . trim($path, DIRECTORY_SEPARATOR);
}
}
Expand Down
18 changes: 18 additions & 0 deletions core/stubs/middleware.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace {namespace};

use Amp\Http\Server\Middleware;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;

class {name} implements Middleware
{
public function handleRequest(Request $request, RequestHandler $next): Response
{
return $next->handleRequest($request);
}
}
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Core">
<directory suffix="Test.php">./tests/Core</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
Expand Down
9 changes: 9 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use App\Http\Controllers\WelcomeController;
use App\Http\Middleware\AcceptJsonResponses;
use Core\Facades\Router;

Router::get('/', [WelcomeController::class, 'index'], new AcceptJsonResponses());
8 changes: 0 additions & 8 deletions routes/web.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Unit\Console;
namespace Tests\Core\Console;

use Core\Contracts\Filesystem\File;

Expand All @@ -11,6 +11,9 @@
exists: fn (string $path) => false,
get: fn (string $path) => '',
put: fn (string $path) => true,
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);
Expand Down Expand Up @@ -58,6 +61,9 @@
exists: fn (string $path) => false,
get: fn (string $path) => 'new content',
put: fn (string $path, string $content) => file_put_contents($tempPath, $content),
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);
Expand Down Expand Up @@ -104,6 +110,9 @@
exists: fn (string $path) => false,
get: fn (string $path) => 'Hello, world!',
put: fn (string $path, string $content) => file_put_contents($tempPath, $content),
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);
Expand Down
Loading