diff --git a/src/ContentParsers/JsonParser.php b/src/ContentParsers/JsonParser.php index 75f5855..fdd7063 100644 --- a/src/ContentParsers/JsonParser.php +++ b/src/ContentParsers/JsonParser.php @@ -3,7 +3,6 @@ namespace Spatie\Sheets\ContentParsers; use Spatie\Sheets\ContentParser; -use Symfony\Component\Yaml\Yaml; class JsonParser implements ContentParser { diff --git a/src/Repositories/FilesystemRepository.php b/src/Repositories/FilesystemRepository.php index e6ab85f..f2bfaf2 100644 --- a/src/Repositories/FilesystemRepository.php +++ b/src/Repositories/FilesystemRepository.php @@ -2,12 +2,12 @@ namespace Spatie\Sheets\Repositories; -use Spatie\Sheets\Sheet; -use Spatie\Sheets\Factory; +use Illuminate\Contracts\Filesystem\Factory as FilesystemManagerContract; +use Illuminate\Support\Collection; use Illuminate\Support\Str; +use Spatie\Sheets\Factory; use Spatie\Sheets\Repository; -use Illuminate\Support\Collection; -use Illuminate\Contracts\Filesystem\Filesystem; +use Spatie\Sheets\Sheet; class FilesystemRepository implements Repository { @@ -20,20 +20,20 @@ class FilesystemRepository implements Repository /** @var string */ protected $extension; - public function __construct(Factory $factory, Filesystem $filesystem, string $extension = 'md') + public function __construct(Factory $factory, FilesystemManagerContract $filesystem, array $config = []) { $this->factory = $factory; - $this->filesystem = $filesystem; - $this->extension = $extension; + $this->filesystem = $filesystem->disk($config['disk'] ?? null); + $this->extension = $config['extension'] ?? 'md'; } public function get(string $path): ?Sheet { - if (!Str::endsWith($path, $this->extension)) { + if (! Str::endsWith($path, $this->extension)) { $path = "{$path}.{$this->extension}"; } - if (!$this->filesystem->exists($path)) { + if (! $this->filesystem->exists($path)) { return null; } diff --git a/src/Sheet.php b/src/Sheet.php index 20420e4..0705814 100644 --- a/src/Sheet.php +++ b/src/Sheet.php @@ -3,11 +3,11 @@ namespace Spatie\Sheets; use ArrayAccess; -use JsonSerializable; -use Illuminate\Support\Str; -use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\Eloquent\JsonEncodingException; +use Illuminate\Support\Str; +use JsonSerializable; class Sheet implements ArrayAccess, Arrayable, Jsonable, JsonSerializable { @@ -46,7 +46,7 @@ public function __toString(): string public function offsetExists($key) { - return !is_null($this->getAttribute($key)); + return ! is_null($this->getAttribute($key)); } public function offsetGet($key) @@ -68,7 +68,7 @@ public function toArray(): array { $keys = array_keys($this->attributes); - return array_map(function(string $key) { + return array_map(function (string $key) { return $this->getAttribute($key); }, array_combine($keys, $keys)); } diff --git a/src/Sheets.php b/src/Sheets.php index 040ee78..8308d17 100644 --- a/src/Sheets.php +++ b/src/Sheets.php @@ -22,7 +22,8 @@ public function collection(string $name): Repository return $this->collections[$name]; } - public function registerCollection(string $name, Repository $repository) { + public function registerCollection(string $name, Repository $repository) + { $this->collections[$name] = $repository; } diff --git a/src/SheetsServiceProvider.php b/src/SheetsServiceProvider.php index 5013a26..cea387c 100644 --- a/src/SheetsServiceProvider.php +++ b/src/SheetsServiceProvider.php @@ -2,7 +2,6 @@ namespace Spatie\Sheets; -use Illuminate\Filesystem\FilesystemManager; use Illuminate\Support\ServiceProvider; use League\CommonMark\CommonMarkConverter; use Spatie\Sheets\ContentParsers\MarkdownParser; @@ -60,11 +59,7 @@ public function register() $config['sheet_class'] ); - $repository = new FilesystemRepository( - $factory, - $this->app->make(FilesystemManager::class)->disk($config['disk']), - $config['extension'] - ); + $repository = $this->app->make($config['repository'], compact('factory', 'config')); $sheets->registerCollection($name, $repository); } @@ -87,6 +82,7 @@ protected function mergeCollectionConfigWithDefaults(string $name, array $config 'path_parser' => SlugParser::class, 'content_parser' => MarkdownWithFrontMatterParser::class, 'extension' => 'md', + 'repository' => FilesystemRepository::class, ]; return array_merge($defaults, $config); diff --git a/tests/Concerns/UsesFilesystem.php b/tests/Concerns/UsesFilesystem.php index d9c659d..b45e9d1 100644 --- a/tests/Concerns/UsesFilesystem.php +++ b/tests/Concerns/UsesFilesystem.php @@ -2,19 +2,33 @@ namespace Spatie\Sheets\Tests\Concerns; -use Illuminate\Contracts\Filesystem\Filesystem; +use Illuminate\Contracts\Filesystem\Factory as FilesystemManagerContract; use Illuminate\Filesystem\FilesystemAdapter; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem as Flysystem; trait UsesFilesystem { - protected function createFilesystem(): Filesystem + protected function createFilesystem(): FilesystemManagerContract { $adapter = new Local(__DIR__.'/../fixtures/content'); $flysystem = new Flysystem($adapter); - return new FilesystemAdapter($flysystem); + $adapter = new FilesystemAdapter($flysystem); + + return new class($adapter) implements FilesystemManagerContract { + private $adapter; + + public function __construct($adapter) + { + $this->adapter = $adapter; + } + + public function disk($name = null) + { + return $this->adapter; + } + }; } } diff --git a/tests/ContentParsers/JsonParserTest.php b/tests/ContentParsers/JsonParserTest.php index 193bc45..12203f3 100644 --- a/tests/ContentParsers/JsonParserTest.php +++ b/tests/ContentParsers/JsonParserTest.php @@ -4,9 +4,6 @@ use PHPUnit\Framework\TestCase; use Spatie\Sheets\ContentParsers\JsonParser; -use Spatie\Sheets\ContentParsers\MarkdownParser; -use League\CommonMark\CommonMarkConverter; -use Spatie\Sheets\ContentParsers\YamlParser; class JsonParserTest extends TestCase { @@ -21,8 +18,8 @@ public function it_converts_a_front_matter_document_to_attributes() 'data' => [ 'foo', 'bar', - ] - ] + ], + ], ]; $this->assertEquals($expected, $jsonParser->parse(json_encode($expected))); diff --git a/tests/ContentParsers/MarkdownParserTest.php b/tests/ContentParsers/MarkdownParserTest.php index 8697293..b5968f2 100644 --- a/tests/ContentParsers/MarkdownParserTest.php +++ b/tests/ContentParsers/MarkdownParserTest.php @@ -2,9 +2,9 @@ namespace Spatie\Sheets\Tests\ContentParsers; +use League\CommonMark\CommonMarkConverter; use PHPUnit\Framework\TestCase; use Spatie\Sheets\ContentParsers\MarkdownParser; -use League\CommonMark\CommonMarkConverter; class MarkdownParserTest extends TestCase { diff --git a/tests/ContentParsers/MarkdownWithFrontMatterParserTest.php b/tests/ContentParsers/MarkdownWithFrontMatterParserTest.php index 0060576..aad5289 100644 --- a/tests/ContentParsers/MarkdownWithFrontMatterParserTest.php +++ b/tests/ContentParsers/MarkdownWithFrontMatterParserTest.php @@ -2,9 +2,9 @@ namespace Spatie\Sheets\Tests\ContentParsers; +use League\CommonMark\CommonMarkConverter; use PHPUnit\Framework\TestCase; use Spatie\Sheets\ContentParsers\MarkdownWithFrontMatterParser; -use League\CommonMark\CommonMarkConverter; class MarkdownWithFrontMatterParserTest extends TestCase { diff --git a/tests/ContentParsers/YamlParserTest.php b/tests/ContentParsers/YamlParserTest.php index 0a63f69..e843449 100644 --- a/tests/ContentParsers/YamlParserTest.php +++ b/tests/ContentParsers/YamlParserTest.php @@ -3,8 +3,6 @@ namespace Spatie\Sheets\Tests\ContentParsers; use PHPUnit\Framework\TestCase; -use Spatie\Sheets\ContentParsers\MarkdownParser; -use League\CommonMark\CommonMarkConverter; use Spatie\Sheets\ContentParsers\YamlParser; class YamlParserTest extends TestCase @@ -28,8 +26,8 @@ public function it_converts_a_front_matter_document_to_attributes() 'data' => [ 'foo', 'bar', - ] - ] + ], + ], ]; $this->assertEquals($expected, $yamlParser->parse($contents)); diff --git a/tests/Integration/CustomContentParserTest.php b/tests/Integration/CustomContentParserTest.php index aa8f1da..6379b22 100644 --- a/tests/Integration/CustomContentParserTest.php +++ b/tests/Integration/CustomContentParserTest.php @@ -2,10 +2,8 @@ namespace Spatie\Sheets\Tests\Integration; -use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Spatie\Sheets\ContentParsers\MarkdownParser; -use Spatie\Sheets\PathParsers\SlugWithDateParser; use Spatie\Sheets\Sheet; use Spatie\Sheets\Sheets; diff --git a/tests/Integration/CustomDiskTest.php b/tests/Integration/CustomDiskTest.php index 2419ffa..d1243ac 100644 --- a/tests/Integration/CustomDiskTest.php +++ b/tests/Integration/CustomDiskTest.php @@ -3,8 +3,8 @@ namespace Spatie\Sheets\Tests\Integration; use Illuminate\Support\Collection; -use Spatie\Sheets\Sheets; use Spatie\Sheets\Sheet; +use Spatie\Sheets\Sheets; class CustomDiskTest extends TestCase { @@ -28,7 +28,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('sheets', [ 'collections' => [ 'posts' => [ - 'disk' => 'content' + 'disk' => 'content', ], ], ]); diff --git a/tests/Integration/CustomRepositoryTest.php b/tests/Integration/CustomRepositoryTest.php new file mode 100644 index 0000000..c124467 --- /dev/null +++ b/tests/Integration/CustomRepositoryTest.php @@ -0,0 +1,57 @@ +app->make(Sheets::class)->all(); + + $this->assertInstanceOf(Collection::class, $documents); + $this->assertCount(2, $documents); + $this->assertContainsOnlyInstancesOf(Sheet::class, $documents); + + $this->assertEquals('foo', $documents[0]->path); + $this->assertEquals('bar', $documents[0]->foo); + + $this->assertEquals('bar', $documents[1]->path); + $this->assertEquals('bar', $documents[1]->foo); + } + + protected function getEnvironmentSetUp($app) + { + $app['config']->set('sheets', [ + 'collections' => [ + 'null' => [ + 'repository' => StaticRepository::class, + ], + ], + ]); + } +} + +final class StaticRepository implements Repository +{ + public function all(): Collection + { + return new Collection([ + $this->get('foo'), + $this->get('bar'), + ]); + } + + public function get(string $path): ?Sheet + { + return new Sheet([ + 'path' => $path, + 'foo' => 'bar', + ]); + } +} diff --git a/tests/Integration/CustomSheetClassTest.php b/tests/Integration/CustomSheetClassTest.php index d14a88c..82feb48 100644 --- a/tests/Integration/CustomSheetClassTest.php +++ b/tests/Integration/CustomSheetClassTest.php @@ -5,7 +5,6 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Spatie\Sheets\PathParsers\SlugWithDateParser; -use Spatie\Sheets\Sheet; use Spatie\Sheets\Sheets; use Spatie\Sheets\Tests\Integration\DummySheets\Post; diff --git a/tests/Integration/DefaultConfigTest.php b/tests/Integration/DefaultConfigTest.php index d005700..a0451a1 100644 --- a/tests/Integration/DefaultConfigTest.php +++ b/tests/Integration/DefaultConfigTest.php @@ -3,8 +3,8 @@ namespace Spatie\Sheets\Tests\Integration; use Illuminate\Support\Collection; -use Spatie\Sheets\Sheets; use Spatie\Sheets\Sheet; +use Spatie\Sheets\Sheets; class DefaultConfigTest extends TestCase { diff --git a/tests/Integration/ExplicitDefaultCollectionTest.php b/tests/Integration/ExplicitDefaultCollectionTest.php index f03fc5a..00a3be6 100644 --- a/tests/Integration/ExplicitDefaultCollectionTest.php +++ b/tests/Integration/ExplicitDefaultCollectionTest.php @@ -2,9 +2,7 @@ namespace Spatie\Sheets\Tests\Integration; -use Illuminate\Support\Collection; use Spatie\Sheets\PathParsers\SlugWithDateParser; -use Spatie\Sheets\Sheet; use Spatie\Sheets\Sheets; use Spatie\Sheets\Tests\Integration\DummySheets\Page; use Spatie\Sheets\Tests\Integration\DummySheets\Post; diff --git a/tests/Integration/ImplicitDefaultCollectionTest.php b/tests/Integration/ImplicitDefaultCollectionTest.php index 5e60bc2..bd62035 100644 --- a/tests/Integration/ImplicitDefaultCollectionTest.php +++ b/tests/Integration/ImplicitDefaultCollectionTest.php @@ -2,9 +2,7 @@ namespace Spatie\Sheets\Tests\Integration; -use Illuminate\Support\Collection; use Spatie\Sheets\PathParsers\SlugWithDateParser; -use Spatie\Sheets\Sheet; use Spatie\Sheets\Sheets; use Spatie\Sheets\Tests\Integration\DummySheets\Page; use Spatie\Sheets\Tests\Integration\DummySheets\Post; diff --git a/tests/Integration/MultipleCollectionsTest.php b/tests/Integration/MultipleCollectionsTest.php index 5e9abba..b8daa29 100644 --- a/tests/Integration/MultipleCollectionsTest.php +++ b/tests/Integration/MultipleCollectionsTest.php @@ -4,7 +4,6 @@ use Illuminate\Support\Collection; use Spatie\Sheets\PathParsers\SlugWithDateParser; -use Spatie\Sheets\Sheet; use Spatie\Sheets\Sheets; use Spatie\Sheets\Tests\Integration\DummySheets\Page; use Spatie\Sheets\Tests\Integration\DummySheets\Post; diff --git a/tests/PathParsers/SlugWithOrderParserTest.php b/tests/PathParsers/SlugWithOrderParserTest.php index dd1a675..709855f 100644 --- a/tests/PathParsers/SlugWithOrderParserTest.php +++ b/tests/PathParsers/SlugWithOrderParserTest.php @@ -2,7 +2,6 @@ namespace Spatie\Sheets\Tests\PathParsers; -use Illuminate\Support\Carbon; use PHPUnit\Framework\TestCase; use Spatie\Sheets\PathParsers\SlugWithOrderParser; diff --git a/tests/Repositories/FilesystemRepositoryTest.php b/tests/Repositories/FilesystemRepositoryTest.php index b8558a4..a12df63 100644 --- a/tests/Repositories/FilesystemRepositoryTest.php +++ b/tests/Repositories/FilesystemRepositoryTest.php @@ -2,10 +2,10 @@ namespace Spatie\Sheets\Tests\Repositories; +use Illuminate\Support\Collection; use PHPUnit\Framework\TestCase; use Spatie\Sheets\Repositories\FilesystemRepository; use Spatie\Sheets\Sheet; -use Illuminate\Support\Collection; use Spatie\Sheets\Tests\Concerns\UsesFactory; use Spatie\Sheets\Tests\Concerns\UsesFilesystem; diff --git a/tests/SheetTest.php b/tests/SheetTest.php index e620735..feb0de2 100644 --- a/tests/SheetTest.php +++ b/tests/SheetTest.php @@ -3,12 +3,12 @@ namespace Spatie\Sheets\Tests; use ArrayAccess; -use ReflectionClass; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; use JsonSerializable; -use Spatie\Sheets\Sheet; use PHPUnit\Framework\TestCase; -use Illuminate\Contracts\Support\Jsonable; -use Illuminate\Contracts\Support\Arrayable; +use ReflectionClass; +use Spatie\Sheets\Sheet; class SheetTest extends TestCase {