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

allow to use a custom repository #45

Merged
merged 2 commits into from Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/ContentParsers/JsonParser.php
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\Sheets\ContentParsers;

use Spatie\Sheets\ContentParser;
use Symfony\Component\Yaml\Yaml;

class JsonParser implements ContentParser
{
Expand Down
18 changes: 9 additions & 9 deletions src/Repositories/FilesystemRepository.php
Expand Up @@ -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
{
Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Sheet.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Sheets.php
Expand Up @@ -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;
}

Expand Down
8 changes: 2 additions & 6 deletions src/SheetsServiceProvider.php
Expand Up @@ -2,7 +2,6 @@

namespace Spatie\Sheets;

use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Support\ServiceProvider;
use League\CommonMark\CommonMarkConverter;
use Spatie\Sheets\ContentParsers\MarkdownParser;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions tests/Concerns/UsesFilesystem.php
Expand Up @@ -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;
}
};
}
}
7 changes: 2 additions & 5 deletions tests/ContentParsers/JsonParserTest.php
Expand Up @@ -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
{
Expand All @@ -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)));
Expand Down
2 changes: 1 addition & 1 deletion tests/ContentParsers/MarkdownParserTest.php
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ContentParsers/MarkdownWithFrontMatterParserTest.php
Expand Up @@ -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
{
Expand Down
6 changes: 2 additions & 4 deletions tests/ContentParsers/YamlParserTest.php
Expand Up @@ -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
Expand All @@ -28,8 +26,8 @@ public function it_converts_a_front_matter_document_to_attributes()
'data' => [
'foo',
'bar',
]
]
],
],
];

$this->assertEquals($expected, $yamlParser->parse($contents));
Expand Down
2 changes: 0 additions & 2 deletions tests/Integration/CustomContentParserTest.php
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CustomDiskTest.php
Expand Up @@ -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
{
Expand All @@ -28,7 +28,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('sheets', [
'collections' => [
'posts' => [
'disk' => 'content'
'disk' => 'content',
],
],
]);
Expand Down
57 changes: 57 additions & 0 deletions tests/Integration/CustomRepositoryTest.php
@@ -0,0 +1,57 @@
<?php

namespace Spatie\Sheets\Tests\Integration;

use Illuminate\Support\Collection;
use Spatie\Sheets\Repository;
use Spatie\Sheets\Sheet;
use Spatie\Sheets\Sheets;

class CustomRepositoryTest extends TestCase
{
/** @test */
public function it_can_maintain_a_collection_with_a_custom_repository()
{
$documents = $this->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',
]);
}
}
1 change: 0 additions & 1 deletion tests/Integration/CustomSheetClassTest.php
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/DefaultConfigTest.php
Expand Up @@ -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
{
Expand Down
2 changes: 0 additions & 2 deletions tests/Integration/ExplicitDefaultCollectionTest.php
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions tests/Integration/ImplicitDefaultCollectionTest.php
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/Integration/MultipleCollectionsTest.php
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/PathParsers/SlugWithOrderParserTest.php
Expand Up @@ -2,7 +2,6 @@

namespace Spatie\Sheets\Tests\PathParsers;

use Illuminate\Support\Carbon;
use PHPUnit\Framework\TestCase;
use Spatie\Sheets\PathParsers\SlugWithOrderParser;

Expand Down
2 changes: 1 addition & 1 deletion tests/Repositories/FilesystemRepositoryTest.php
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions tests/SheetTest.php
Expand Up @@ -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
{
Expand Down