Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding scopes and other improvements #16

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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: 0 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ jobs:
with:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2']
stability: >-
['prefer-lowest', 'prefer-stable']
2 changes: 0 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ jobs:
with:
os: >-
['ubuntu-latest']
php: >-
['8.1']
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ composer.phar
Thumbs.db

# Other
.phpunit.result.cache
.php_cs.cache
clover.xml
.env
builds
build
.phpunit.cache
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
"ext-json": "*",
"spiral/attributes": "^2.10 || ^3.0",
"spiral/boot": "^3.0",
"spiral/core": "^3.12",
"spiral/data-grid": "^3.0",
"spiral/http": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.20",
"phpunit/phpunit": "^10.5",
"spiral/testing": "dev-feature/scopes as 2.8.0",
"spiral/hmvc": "^3.0",
"vimeo/psalm": "^4.27"
"vimeo/psalm": "^5.22",
"spiral/framework": "dev-feature/http-scopes as 3.13.0"
},
"autoload": {
"psr-4": {
Expand Down
29 changes: 20 additions & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
failOnRisky="false"
stopOnFailure="false"
stopOnError="false"
stderr="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<coverage>
<include>
<directory>src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
Expand All @@ -27,4 +30,12 @@
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
</php>
<source>
<include>
<directory>src</directory>
</include>
<exclude>
<directory>tests</directory>
</exclude>
</source>
</phpunit>
4 changes: 3 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
errorLevel="2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
8 changes: 7 additions & 1 deletion src/Annotation/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
* @Annotation
* @NamedArgumentConstructor
* @Target({"METHOD"})
*
* @Annotation\Attributes({
*
* @Annotation\Attribute("grid", required=true, type="string"),
* @Annotation\Attribute("view", type="string"),
* @Annotation\Attribute("defaults", type="array"),
* @Annotation\Attribute("options", type="array"),
* @Annotation\Attribute("factory", type="string")
* })
*
* @psalm-suppress DeprecatedClass
*/
#[\Attribute(\Attribute::TARGET_METHOD), NamedArgumentConstructor]
class DataGrid
Expand All @@ -26,8 +30,10 @@ class DataGrid
* @psalm-param non-empty-string $grid Points to grid schema
* @psalm-param non-empty-string|null $view Response options,
* default to GridSchema->__invoke() if such method exists
*
* @param array $defaults Response options, default to GridSchema->getDefaults() if such method exists
* @param array $options Response options, default to GridSchema->getOptions() if such method exists
* @param array $options Response options, default to GridSchema->getOptions() if such method exists
*
* @psalm-param class-string|null $factory Custom user GridFactory
*/
public function __construct(
Expand Down
30 changes: 19 additions & 11 deletions src/Bootloader/GridBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Append;
use Spiral\DataGrid\GridFactoryInterface;
use Spiral\Core\BinderInterface;
use Spiral\Core\Config\Proxy;
use Spiral\DataGrid\Compiler;
use Spiral\DataGrid\Config\GridConfig;
use Spiral\DataGrid\Grid;
use Spiral\DataGrid\GridFactory;
use Spiral\DataGrid\GridFactoryInterface;
use Spiral\DataGrid\GridInput;
use Spiral\DataGrid\GridInterface;
use Spiral\DataGrid\InputInterface;
Expand All @@ -21,23 +23,29 @@

final class GridBootloader extends Bootloader
{
protected const SINGLETONS = [
InputInterface::class => GridInput::class,
GridInterface::class => Grid::class,
GridFactoryInterface::class => GridFactory::class,
Compiler::class => [self::class, 'compiler'],
GridResponseInterface::class => GridResponse::class,
];

public function __construct(
private readonly ConfiguratorInterface $config
private readonly ConfiguratorInterface $config,
private readonly BinderInterface $binder,
) {
}

public function defineSingletons(): array
{
$this->binder->getBinder('http.request')->bindSingleton(InputInterface::class, GridInput::class);
$this->binder->bindSingleton(InputInterface::class, new Proxy(InputInterface::class, true));

return [
GridInterface::class => Grid::class,
GridFactoryInterface::class => GridFactory::class,
Compiler::class => [self::class, 'compiler'],
GridResponseInterface::class => GridResponse::class,
];
}

public function init(): void
{
$this->config->setDefaults(GridConfig::CONFIG, [
'writers' => []
'writers' => [],
]);
}

Expand Down
1 change: 0 additions & 1 deletion src/Config/GridConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ final class GridConfig extends InjectableConfig
{
public const CONFIG = 'dataGrid';

/** @var array */
protected array $config = [
'writers' => [],
];
Expand Down
4 changes: 3 additions & 1 deletion src/GridInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Spiral\DataGrid;

use Spiral\Core\Attribute\Scope;
use Spiral\Http\Request\InputManager;

#[Scope('http.request')]
final class GridInput implements InputInterface
{
public function __construct(
private InputManager $input
private InputManager $input,
) {
}

Expand Down
9 changes: 7 additions & 2 deletions src/Interceptor/GridInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Psr\Container\ContainerInterface;
use Spiral\Attributes\ReaderInterface;
use Spiral\Core\Attribute\Proxy;
use Spiral\Core\CoreInterceptorInterface;
use Spiral\Core\CoreInterface;
use Spiral\DataGrid\Annotation\DataGrid;
Expand All @@ -24,7 +25,7 @@ final class GridInterceptor implements CoreInterceptorInterface

public function __construct(
private readonly GridResponseInterface $response,
private readonly ContainerInterface $container,
#[Proxy] private readonly ContainerInterface $container,
private readonly GridFactory $gridFactory,
private readonly ReaderInterface $reader
) {
Expand Down Expand Up @@ -66,9 +67,13 @@ private function getSchema(string $controller, string $action): ?array
}

$this->cache[$key] = null;
if (!\class_exists($controller)) {
return null;
}

try {
$method = new \ReflectionMethod($controller, $action);
} catch (\ReflectionException $e) {
} catch (\ReflectionException) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Response/GridResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function jsonSerialize(): array
if ($this->grid === null) {
return [
'status' => 500,
'error' => 'missing-grid-source',
'error' => 'missing-grid-source',
];
}

$response = [
'status' => $this->option('status', 200),
'status' => $this->option('status', 200),
$this->option('property', 'data') => $this->data,
];

Expand All @@ -47,7 +47,7 @@ public function jsonSerialize(): array
return $response;
}

private function option(string $name, $default)
private function option(string $name, mixed $default): mixed
{
return $this->options[$name] ?? $default;
}
Expand Down
31 changes: 0 additions & 31 deletions tests/Bootloader/GridBootloaderTest.php

This file was deleted.

77 changes: 77 additions & 0 deletions tests/Feature/Bootloader/GridBootloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\DataGrid\Feature\Bootloader;

use Spiral\Config\ConfigManager;
use Spiral\Config\LoaderInterface;
use Spiral\DataGrid\Bootloader\GridBootloader;
use Spiral\DataGrid\Compiler;
use Spiral\DataGrid\Config\GridConfig;
use Spiral\DataGrid\Grid;
use Spiral\DataGrid\GridFactory;
use Spiral\DataGrid\GridFactoryInterface;
use Spiral\DataGrid\GridInput;
use Spiral\DataGrid\GridInterface;
use Spiral\DataGrid\InputInterface;
use Spiral\DataGrid\Response\GridResponse;
use Spiral\DataGrid\Response\GridResponseInterface;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Tests\DataGrid\Feature\TestCase;

final class GridBootloaderTest extends TestCase
{
public function testInputInterfaceBindingInRootScope(): void
{
$this->assertContainerBoundAsSingleton(InputInterface::class, InputInterface::class);
}

#[TestScope('http.request')]
public function testGridInputInHttpRequestScope(): void
{
$this->assertContainerBoundAsSingleton(InputInterface::class, GridInput::class);
}

public function testGridInterfaceBinding(): void
{
$this->assertContainerBoundAsSingleton(GridInterface::class, Grid::class);
}

public function testGridFactoryInterfaceBinding(): void
{
$this->assertContainerBoundAsSingleton(GridFactoryInterface::class, GridFactory::class);
}

public function testCompilerBinding(): void
{
$this->assertContainerBoundAsSingleton(Compiler::class, Compiler::class);
}

public function testGridResponseInterfaceBinding(): void
{
$this->assertContainerBoundAsSingleton(GridResponseInterface::class, GridResponse::class);
}

public function testDefaultConfig(): void
{
$this->assertConfigMatches(GridConfig::CONFIG, ['writers' => []]);
}

public function testAddWriter(): void
{
$loader = $this->createMock(LoaderInterface::class);
$loader->method('has')->willReturn(true);
$loader->method('load')->willReturn(['writers' => []]);

$configManager = new ConfigManager($loader);

$this->assertSame(['writers' => []], $configManager->getConfig(GridConfig::CONFIG));

$bootloader = new GridBootloader($configManager, $this->getContainer());
$bootloader->addWriter('foo');
$bootloader->addWriter('bar');

$this->assertSame(['writers' => ['foo', 'bar']], $configManager->getConfig(GridConfig::CONFIG));
}
}
Loading
Loading