Skip to content

Commit

Permalink
Avoid Path Resolver; add config factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Feb 1, 2024
1 parent 84a3e36 commit 67f1cf2
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/PhpFileSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Cycle\Schema\Provider;

use Closure;
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,15 +19,41 @@ 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();
$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(

Check warning on line 41 in src/PhpFileSchemaProvider.php

View check run for this annotation

Codecov / codecov/patch

src/PhpFileSchemaProvider.php#L41

Added line #L41 was not covered by tests
string $file,
int $mode = self::MODE_READ_AND_WRITE,
): array {
return [
'file' => $file,
'mode' => $mode,
];

Check warning on line 48 in src/PhpFileSchemaProvider.php

View check run for this annotation

Codecov / codecov/patch

src/PhpFileSchemaProvider.php#L45-L48

Added lines #L45 - L48 were not covered by tests
}

/**
* @param array{
* file: non-empty-string,
* mode?: self::MODE_READ_AND_WRITE|self::MODE_WRITE_ONLY,
* } $config
*/
public function withConfig(array $config): self
{
$new = clone $this;
Expand All @@ -37,7 +62,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 +92,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 +128,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

0 comments on commit 67f1cf2

Please sign in to comment.