Skip to content

Commit

Permalink
Merge pull request #1 from cycle/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
msmakouz committed Feb 2, 2024
2 parents 84a3e36 + edcf429 commit e0237e1
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 94 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ use Cycle\Schema\Provider\SimpleCacheSchemaProvider;
use Cycle\Schema\Provider\Support\SchemaProviderPipeline;

$pipeline = (new SchemaProviderPipeline($container))->withConfig([
SimpleCacheSchemaProvider::class => ['key' => 'cycle-schema'],
FromFilesSchemaProvider::class => ['files' => ['runtime/schema1.php', 'runtime/schema2.php']],
SimpleCacheSchemaProvider::class => SimpleCacheSchemaProvider::config(key: 'cycle-schema'),
FromFilesSchemaProvider::class => FromFilesSchemaProvider::config(files: [
'runtime/schema1.php',
'runtime/schema2.php',
]),
]);

$schema = new Schema($pipeline->read());
Expand All @@ -73,9 +76,12 @@ use Cycle\Schema\Provider\Support\MergeSchemaProvider;
use Cycle\Schema\Provider\Support\SchemaProviderPipeline;

$pipeline = (new SchemaProviderPipeline($container))->withConfig([
SimpleCacheSchemaProvider::class => ['key' => 'cycle-schema'],
SimpleCacheSchemaProvider::class => SimpleCacheSchemaProvider::config(key: 'cycle-schema'),
MergeSchemaProvider::class => [
FromFilesSchemaProvider::class => ['files' => ['runtime/schema1.php', 'runtime/schema2.php']],
FromFilesSchemaProvider::class => FromFilesSchemaProvider::config(files: [
'runtime/schema1.php',
'runtime/schema2.php',
]),
CustomSchemaProvider::class => ['some' => 'config'],
],
]);
Expand Down
40 changes: 30 additions & 10 deletions src/FromFilesSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Cycle\Schema\Provider\Exception\ConfigurationException;
use Cycle\Schema\Provider\Exception\SchemaFileNotFoundException;
use Cycle\Schema\Provider\Path\ResolverInterface;
use Cycle\Schema\Provider\Path\SimpleResolver;
use Webmozart\Glob\Glob;
use Webmozart\Glob\Iterator\GlobIterator;
use Cycle\Schema\Provider\Support\SchemaMerger;
Expand All @@ -18,7 +16,7 @@
final class FromFilesSchemaProvider implements SchemaProviderInterface
{
/**
* @var array<string> Schema files
* @var array<non-empty-string> Schema files
*/
private array $files = [];

Expand All @@ -27,11 +25,33 @@ final class FromFilesSchemaProvider implements SchemaProviderInterface
*/
private bool $strict = false;

private ResolverInterface $pathResolver;
/**
* @var \Closure(non-empty-string): non-empty-string
*/
private \Closure $pathResolver;

public function __construct(?ResolverInterface $resolver = null)
/**
* @param null|callable(non-empty-string): non-empty-string $pathResolver A function that resolves
* framework-specific file paths.
*/
public function __construct(?callable $pathResolver = null)
{
/** @psalm-suppress PropertyTypeCoercion */
$this->pathResolver = $pathResolver === null
? static fn (string $path): string => $path
: \Closure::fromCallable($pathResolver);
}

/**
* Create a configuration array for the {@see self::withConfig()} method.
* @param array<non-empty-string> $files
*/
public static function config(array $files, bool $strict = false): array
{
$this->pathResolver = $resolver ?? new SimpleResolver();
return [
'files' => $files,
'strict' => $strict,
];
}

public function withConfig(array $config): self
Expand All @@ -50,11 +70,11 @@ public function withConfig(array $config): self
}

$files = \array_map(
function ($file) {
if (!\is_string($file)) {
throw new ConfigurationException('The `files` parameter must contain string values.');
function (mixed $file) {
if (!\is_string($file) || $file === '') {
throw new ConfigurationException('The `files` parameter must contain non-empty string values.');
}
return $this->pathResolver->resolve($file);
return ($this->pathResolver)($file);
},
$files
);
Expand Down
10 changes: 0 additions & 10 deletions src/Path/ResolverInterface.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Path/SimpleResolver.php

This file was deleted.

49 changes: 34 additions & 15 deletions src/PhpFileSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Cycle\Schema\Provider\Exception\ConfigurationException;
use Cycle\Schema\Provider\Exception\SchemaProviderException;
use Cycle\Schema\Provider\Path\ResolverInterface;
use Cycle\Schema\Provider\Path\SimpleResolver;
use Cycle\Schema\Renderer\PhpSchemaRenderer;
use Spiral\Files\Files;
use Spiral\Files\FilesInterface;
Expand All @@ -20,13 +18,34 @@ final class PhpFileSchemaProvider implements SchemaProviderInterface
private string $file = '';
private int $mode = self::MODE_READ_AND_WRITE;

private ResolverInterface $pathResolver;
/**
* @var \Closure(non-empty-string): non-empty-string
*/
private \Closure $pathResolver;
private FilesInterface $files;

public function __construct(?ResolverInterface $resolver = null, ?FilesInterface $files = null)
/**
* @param null|callable(non-empty-string): non-empty-string $pathResolver A function that resolves
* framework-specific file paths.
*/
public function __construct(?callable $pathResolver = null, ?FilesInterface $files = null)
{
$this->pathResolver = $resolver ?? new SimpleResolver();
$this->files = $files ?? new Files();
/** @psalm-suppress PropertyTypeCoercion */
$this->pathResolver = $pathResolver === null
? static fn (string $path): string => $path
: \Closure::fromCallable($pathResolver);
}

/**
* Create a configuration array for the {@see self::withConfig()} method.
*/
public static function config(string $file, int $mode = self::MODE_READ_AND_WRITE): array
{
return [
'file' => $file,
'mode' => $mode,
];
}

public function withConfig(array $config): self
Expand All @@ -37,7 +56,7 @@ public function withConfig(array $config): self
if ($this->file === '' && !array_key_exists('file', $config)) {
throw new ConfigurationException('The `file` parameter is required.');
}
$new->file = $this->pathResolver->resolve($config['file']);
$new->file = ($this->pathResolver)($config['file']);

$new->mode = $config['mode'] ?? $this->mode;

Expand Down Expand Up @@ -67,6 +86,15 @@ public function read(?SchemaProviderInterface $nextProvider = null): ?array
return $schema;
}

public function clear(): bool
{
try {
return $this->removeFile();
} catch (\Throwable $e) {
return false;
}
}

private function write(array $schema): bool
{
if (\basename($this->file) === '') {
Expand Down Expand Up @@ -94,15 +122,6 @@ private function removeFile(): bool
return $this->files->delete($this->file);
}

public function clear(): bool
{
try {
return $this->removeFile();
} catch (\Throwable $e) {
return false;
}
}

private function isReadable(): bool
{
return $this->mode !== self::MODE_WRITE_ONLY;
Expand Down
15 changes: 15 additions & 0 deletions src/SimpleCacheSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@
final class SimpleCacheSchemaProvider implements SchemaProviderInterface
{
public const DEFAULT_KEY = 'Cycle-ORM-Schema';

/**
* @var non-empty-string
*/
private string $key = self::DEFAULT_KEY;

public function __construct(
private CacheInterface $cache
) {
}

/**
* Create a configuration array for the {@see self::withConfig()} method.
* @param non-empty-string $key
*/
public static function config(string $key): array
{
return [
'key' => $key,
];
}

public function withConfig(array $config): self
{
$new = clone $this;
Expand Down
54 changes: 30 additions & 24 deletions tests/Feature/FromFilesSchemaProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@
use Cycle\Schema\Provider\Exception\SchemaFileNotFoundException;
use Cycle\Schema\Provider\FromFilesSchemaProvider;

class FromFilesSchemaProviderTest extends BaseSchemaProvider
final class FromFilesSchemaProviderTest extends BaseSchemaProvider
{
protected const READ_CONFIG = ['files' => [__DIR__ . '/Stub/FromFilesSchemaProvider/schema1.php']];
protected const READ_CONFIG_SCHEMA = ['user' => []];

public static function EmptyConfigProvider(): array
public function testConfig(): void
{
return [
[
[],
],
[
['files' => []],
],
];
$this->assertSame(
['files' => ['foo', 'bar'], 'strict' => false],
FromFilesSchemaProvider::config(['foo', 'bar'])
);

$this->assertSame(
['files' => ['foo', 'bar'], 'strict' => true],
FromFilesSchemaProvider::config(['foo', 'bar'], true)
);
}

/**
* @dataProvider EmptyConfigProvider
* @dataProvider emptyConfigProvider
*/
public function testWithConfigEmpty(array $config): void
{
Expand All @@ -48,26 +49,15 @@ public function testWithConfigInvalidFiles(): void
$schemaProvider->withConfig(['files' => __DIR__ . '/Stub/FromFilesSchemaProvider/schema1.php']);
}

public static function FileListBadValuesProvider(): array
{
return [
[null],
[42],
[STDIN],
[[]],
[new \SplFileInfo(__FILE__)],
];
}

/**
* @dataProvider FileListBadValuesProvider
* @dataProvider fileListBadValuesProvider
*/
public function testWithConfigInvalidValueInFileList($value): void
{
$schemaProvider = $this->createSchemaProvider();

$this->expectException(ConfigurationException::class);
$this->expectExceptionMessage('The `files` parameter must contain string values.');
$this->expectExceptionMessage('The `files` parameter must contain non-empty string values.');
$schemaProvider->withConfig(['files' => [$value]]);
}

Expand Down Expand Up @@ -204,6 +194,22 @@ public function testClear(): void
$this->assertFalse($schemaProvider->clear());
}

public static function emptyConfigProvider(): \Traversable
{
yield [[]];
yield [['files' => []]];
}

public static function fileListBadValuesProvider(): \Traversable
{
yield [null];
yield [42];
yield [STDIN];
yield [[]];
yield [['']];
yield [new \SplFileInfo(__FILE__)];
}

protected function createSchemaProvider(array $config = null): FromFilesSchemaProvider
{
$provider = new FromFilesSchemaProvider();
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/PhpFileSchemaProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ protected function tearDown(): void
$this->removeTmpFile();
}

public function testConfig(): void
{
$this->assertSame(
['file' => 'foo', 'mode' => PhpFileSchemaProvider::MODE_READ_AND_WRITE],
PhpFileSchemaProvider::config('foo')
);

$this->assertSame(
['file' => 'foo', 'mode' => PhpFileSchemaProvider::MODE_WRITE_ONLY],
PhpFileSchemaProvider::config('foo', PhpFileSchemaProvider::MODE_WRITE_ONLY)
);
}

public function testReadFromNextProvider(): void
{
$provider1 = $this->createSchemaProvider(self::WRITE_CONFIG);
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/SimpleCacheSchemaProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ final class SimpleCacheSchemaProviderTest extends BaseSchemaProvider

private SimpleCacheActionLogger $cacheService;

public function testConfig(): void
{
$this->assertSame(
['key' => 'foo'],
SimpleCacheSchemaProvider::config('foo')
);
}

public function testDefaultState(): void
{
$provider = $this->createSchemaProvider();
Expand Down
18 changes: 0 additions & 18 deletions tests/Unit/Path/SimpleResolverTest.php

This file was deleted.

0 comments on commit e0237e1

Please sign in to comment.