Skip to content

Commit 3c38011

Browse files
authored
Merge pull request #15 from MaplePHP/develop
Syntax improvements and code refactor
2 parents 9ae7001 + 86e3a22 commit 3c38011

File tree

16 files changed

+155
-40
lines changed

16 files changed

+155
-40
lines changed

.github/workflows/php.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: PHP Unitary
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
env:
17+
COMPOSER_ROOT_VERSION: 2.x-dev
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Cache Composer packages
23+
uses: actions/cache@v3
24+
with:
25+
path: vendor
26+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
27+
restore-keys: |
28+
${{ runner.os }}-php-
29+
30+
- name: Install dependencies
31+
run: composer install --prefer-dist --no-progress
32+
33+
- name: Run test suite
34+
run: php bin/unitary

bin/unitary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ $app = (new Application())
2828
->boot([
2929
"argv" => $argv,
3030
"dir" => getcwd()
31-
]);
31+
]);

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"homepage": "https://maplephp.github.io/Unitary/"
2222
}
2323
],
24+
"scripts": {
25+
"test": "php bin/unitary"
26+
},
2427
"require": {
2528
"php": ">=8.0",
2629
"composer/semver": "^3.4",
@@ -45,5 +48,12 @@
4548
"bin": [
4649
"bin/unitary"
4750
],
48-
"minimum-stability": "stable"
51+
"extra": {
52+
"branch-alias": {
53+
"dev-main": "2.x-dev",
54+
"dev-develop": "2.x-dev"
55+
}
56+
},
57+
"minimum-stability": "dev",
58+
"prefer-stable": true
4959
}

src/Config/ConfigProps.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@
1919
*/
2020
class ConfigProps extends AbstractConfigProps
2121
{
22-
public ?string $path = null;
2322
public ?string $discoverPattern = null;
2423
public ?string $exclude = null;
2524
public ?string $show = null;
2625
public ?string $timezone = null;
2726
public ?string $locale = null;
2827
public ?string $type = null;
28+
public ?string $path = null;
2929
public ?int $exitCode = null;
3030
public ?bool $verbose = null;
3131
public ?bool $alwaysShowFiles = null;
3232
public ?bool $errorsOnly = null;
3333
public ?bool $smartSearch = null;
3434
public ?bool $failFast = null;
3535
public ?string $helpController = null;
36+
public ?array $configuration = null;
3637

3738
/**
3839
* Hydrate the properties/object with expected data, and handle unexpected data
@@ -93,6 +94,9 @@ protected function propsHydration(string|bool $key, mixed $value): void
9394
case 'failFast':
9495
$this->failFast = $this->dataToBool($value);
9596
break;
97+
case 'configuration':
98+
$this->configuration = (array)$value;
99+
break;
96100
}
97101
}
98102

src/Console/Application.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717
final class Application
1818
{
19+
private array $middlewares = [
20+
AddCommandMiddleware::class,
21+
ConfigPropsMiddleware::class,
22+
CheckAllowedProps::class,
23+
LocalMiddleware::class,
24+
CliInitMiddleware::class
25+
];
26+
1927
public function __construct()
2028
{
2129
// Default config
@@ -29,6 +37,49 @@ public function __construct()
2937
EmitronKernel::setRouterFilePath(__DIR__ . "/ConsoleRouter.php");
3038
}
3139

40+
/**
41+
* Clear the default middlewares, be careful with this
42+
*
43+
* @return $this
44+
*/
45+
public function clearDefaultMiddleware(): self
46+
{
47+
$inst = clone $this;
48+
$inst->middlewares = [];
49+
return $inst;
50+
}
51+
52+
/**
53+
* Clear the default middlewares, be careful with this
54+
*
55+
* @return $this
56+
*/
57+
public function unsetMiddleware(string $class): self
58+
{
59+
60+
$inst = clone $this;
61+
foreach($inst->middlewares as $key => $middleware) {
62+
if($middleware === $class) {
63+
unset($inst->middlewares[$key]);
64+
break;
65+
}
66+
}
67+
return $inst;
68+
}
69+
70+
/**
71+
* Add custom middlewares, follow PSR convention
72+
*
73+
* @param array $middleware
74+
* @return $this
75+
*/
76+
public function withMiddleware(array $middleware): self
77+
{
78+
$inst = clone $this;
79+
$inst->middlewares = array_merge($inst->middlewares, $middleware);
80+
return $inst;
81+
}
82+
3283
/**
3384
* Change router file
3485
*
@@ -84,13 +135,7 @@ public function boot(array $parts): Kernel
84135
{
85136
$env = new Environment();
86137
$request = new ServerRequest(new Uri($env->getUriParts($parts)), $env);
87-
$kernel = new Kernel(new Container(), [
88-
AddCommandMiddleware::class,
89-
ConfigPropsMiddleware::class,
90-
CheckAllowedProps::class,
91-
LocalMiddleware::class,
92-
CliInitMiddleware::class
93-
]);
138+
$kernel = new Kernel(new Container(), $this->middlewares);
94139
$kernel->run($request);
95140
return $kernel;
96141
}

src/Console/Controllers/DefaultController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MaplePHP\Unitary\Console\Controllers;
44

5+
use MaplePHP\Emitron\Contracts\ConfigPropsInterface;
56
use Psr\Container\ContainerExceptionInterface;
67
use Psr\Container\ContainerInterface;
78
use Psr\Container\NotFoundExceptionInterface;
@@ -10,7 +11,6 @@
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
1213
use MaplePHP\Prompts\Command;
13-
use MaplePHP\Unitary\Config\ConfigProps;
1414
use MaplePHP\Validate\Validator;
1515

1616
abstract class DefaultController
@@ -20,7 +20,7 @@ abstract class DefaultController
2020
protected Command $command;
2121
protected DispatchConfigInterface $configs;
2222
protected array $args;
23-
protected ?ConfigProps $props = null;
23+
protected ?ConfigPropsInterface $props = null;
2424
protected string|bool $path;
2525

2626
/**

src/Console/Kernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Exception;
1818
use MaplePHP\Emitron\Contracts\DispatchConfigInterface;
1919
use MaplePHP\Emitron\DispatchConfig;
20+
use MaplePHP\Unitary\Config\ConfigProps;
2021
use Psr\Http\Message\ServerRequestInterface;
2122
use Psr\Http\Message\StreamInterface;
2223
use MaplePHP\Unitary\Support\Router;
@@ -72,7 +73,7 @@ public function run(ServerRequestInterface $request, ?StreamInterface $stream =
7273
*/
7374
private function configuration(ServerRequestInterface $request): DispatchConfigInterface
7475
{
75-
$config = new DispatchConfig(EmitronKernel::getConfigFilePath());
76+
$config = new DispatchConfig(EmitronKernel::getConfigFilePath(), ConfigProps::class);
7677
return $config
7778
->setRouter(function ($routerFile) use ($request) {
7879
$router = new Router($request->getCliKeyword(), $request->getCliArgs());

src/Console/Middlewares/ConfigPropsMiddleware.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MaplePHP\Unitary\Console\Middlewares;
44

5+
use MaplePHP\Emitron\Configs\ConfigPropsFactory;
6+
use MaplePHP\Emitron\Contracts\ConfigPropsInterface;
57
use Psr\Container\ContainerExceptionInterface;
68
use Psr\Container\ContainerInterface;
79
use Psr\Container\NotFoundExceptionInterface;
@@ -15,7 +17,7 @@
1517
class ConfigPropsMiddleware implements MiddlewareInterface
1618
{
1719

18-
protected ?ConfigProps $props = null;
20+
protected ?ConfigPropsInterface $props = null;
1921
private ContainerInterface $container;
2022

2123
/**
@@ -56,7 +58,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5658
* @throws ContainerExceptionInterface
5759
* @throws NotFoundExceptionInterface
5860
*/
59-
private function getInitProps(): ConfigProps
61+
private function getInitProps(): ConfigPropsInterface
6062
{
6163
if ($this->props === null) {
6264
$args = $this->container->get("args");
@@ -65,14 +67,13 @@ private function getInitProps(): ConfigProps
6567

6668
try {
6769
$props = array_merge($configs->getProps()->toArray(), $args);
68-
$this->props = new ConfigProps($props);
69-
70+
$this->props = ConfigPropsFactory::create($props, $configs->getConfigPropsClass());
7071
if ($this->props->hasMissingProps() !== [] && isset($args['verbose'])) {
7172
$command->error('The properties (' .
72-
implode(", ", $this->props->hasMissingProps()) . ') is not exist in config props');
73+
implode(", ", $this->props->hasMissingProps()) . ') is not exist in ' . get_class($this->props));
7374
$command->message(
7475
"One or more arguments you passed are not recognized as valid options.\n" .
75-
"Check your command syntax or configuration."
76+
"Check your command parameter syntax for spellings or configuration."
7677
);
7778
}
7879

src/Console/Middlewares/LocalMiddleware.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ public function __construct(ContainerInterface $container)
3939
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
4040
{
4141
$props = $this->container->get("props");
42-
Clock::setDefaultLocale($props->locale);
43-
Clock::setDefaultTimezone($props->timezone);
42+
if($props->locale !== null) {
43+
Clock::setDefaultLocale($props->locale);
44+
}
45+
if($props->timezone !== null) {
46+
Clock::setDefaultTimezone($props->timezone);
47+
}
4448
return $handler->handle($request);
4549
}
4650
}

src/Console/Services/AbstractMainService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MaplePHP\Unitary\Console\Services;
44

5+
use MaplePHP\Emitron\Contracts\ConfigPropsInterface;
56
use Psr\Container\ContainerExceptionInterface;
67
use Psr\Container\ContainerInterface;
78
use Psr\Container\NotFoundExceptionInterface;
@@ -10,7 +11,6 @@
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
1213
use MaplePHP\Prompts\Command;
13-
use MaplePHP\Unitary\Config\ConfigProps;
1414

1515
abstract class AbstractMainService
1616
{
@@ -20,7 +20,7 @@ abstract class AbstractMainService
2020
protected Command $command;
2121
protected DispatchConfigInterface $configs;
2222
protected ServerRequestInterface|RequestInterface $request;
23-
protected ?ConfigProps $props = null;
23+
protected ?ConfigPropsInterface $props = null;
2424

2525
/**
2626
* @throws NotFoundExceptionInterface

0 commit comments

Comments
 (0)