Skip to content

Commit

Permalink
Added configurable nesting optinos
Browse files Browse the repository at this point in the history
  • Loading branch information
IsraelOrtuno committed Sep 15, 2019
1 parent e500785 commit cba8b60
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 27 deletions.
7 changes: 3 additions & 4 deletions config/permalink.php
Expand Up @@ -37,9 +37,8 @@
| Check the NestingService class to understand how it works.
*/

'nesting' => [
'regenerate_children_path_on_update' => true
],
'nest_to_parent_on_create' => true,
'rebuild_children_final_path_on_update' => true,


/*
Expand Down Expand Up @@ -70,4 +69,4 @@
*/

'refresh_route_lookups' => true
];
];
14 changes: 14 additions & 0 deletions src/Contracts/PathBuilder.php
@@ -0,0 +1,14 @@
<?php

namespace Devio\Permalink\Contracts;

interface PathBuilder
{
public function build($model);

public function single($model);

public function recursive($model);

public function all();
}
33 changes: 26 additions & 7 deletions src/PermalinkObserver.php
Expand Up @@ -2,23 +2,34 @@

namespace Devio\Permalink;

use Devio\Permalink\Services\NestingService;
use Devio\Permalink\Services\PathBuilder;
use Cviebrock\EloquentSluggable\Services\SlugService;

class PermalinkObserver
{
/**
* The PathBuilder instance.
*
* @var PathBuilder
*/
protected $path;

/**
* The SlugService instance.
*
* @var SlugService
*/
private $slugService;
protected $slugService;

/**
* PermalinkObserver constructor.
*
* @param PathBuilder $path
* @param SlugService $slugService
*/
public function __construct(SlugService $slugService)
public function __construct(PathBuilder $path, SlugService $slugService)
{
$this->path = $path;
$this->slugService = $slugService;
}

Expand All @@ -37,8 +48,7 @@ public function creating($permalink)
$this->slugService->slug($permalink);
}

(new NestingService)->single($permalink);
}
$this->path->build($permalink);
}

/**
Expand All @@ -50,7 +60,10 @@ public function updating($permalink)
{
if ($permalink->getOriginal('slug') !== $permalink->slug) {
$this->ensureSlugIsUnique($permalink);
(new NestingService)->single($permalink);

config('rebuild_children_final_path_on_update')
? $this->path->recursive($permalink)
: $this->path->single($permalink);
}
}

Expand Down Expand Up @@ -78,7 +91,13 @@ protected function ensureSlugIsUnique($permalink)
*/
protected function nestToParent($permalink)
{
if (! $permalink->exists && $permalink->entity && $parent = NestingService::parentFor($permalink->entity)) {
if (! config('permalink.nest_to_parent_on_create')) {
return;
}

$parent = PathBuilder::parentFor($permalink->entity);

if (! $permalink->exists && $permalink->entity && $parent) {
$permalink->parent_id = $parent->getKey();
$permalink->setRelation('parent', $parent);
}
Expand Down
9 changes: 7 additions & 2 deletions src/PermalinkServiceProvider.php
Expand Up @@ -2,9 +2,10 @@

namespace Devio\Permalink;

use Devio\Permalink\Routing\Router;
use Illuminate\Support\Arr;
use Devio\Permalink\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Devio\Permalink\Contracts\PathBuilder;
use Devio\Permalink\Contracts\NameResolver;
use Devio\Permalink\Contracts\ActionFactory;
use Arcanedev\SeoHelper\Contracts\SeoHelper;
Expand Down Expand Up @@ -78,7 +79,11 @@ public function register()
});

$this->app->singleton(NameResolver::class, function () {
return new Services\NameResolver();
return new Services\NameResolver;
});

$this->app->singleton(PathBuilder::class, function() {
return new Services\PathBuilder;
});

$this->app->singleton(ActionFactory::class, function () {
Expand Down
15 changes: 7 additions & 8 deletions src/Services/NestingService.php → src/Services/PathBuilder.php
Expand Up @@ -3,27 +3,24 @@
namespace Devio\Permalink\Services;

use Devio\Permalink\Permalink;
use Devio\Permalink\Contracts\PathBuilder as Builder;

class NestingService
class PathBuilder implements Builder
{
protected $cache = [];

public function nest($model)
public function build($model)
{
if (! $model->exists) {
return $this->single($model);
} elseif ($model->isDirty('slug')) {
return $this->recursive($model);
}

return $model;
}

public function single($model)
{
$model->final_path = $this->getFullyQualifiedPath($model);

return $model;
}

public function all()
Expand Down Expand Up @@ -51,8 +48,6 @@ public function recursive($model)
$permalink->final_path = $model->final_path . '/' . $permalink->slug;
$permalink->save();
});

return $model;
}

/**
Expand All @@ -74,6 +69,10 @@ public function getFullyQualifiedPath($model)
*/
public static function parentFor($model)
{
if (is_null($model) || (! is_object($model) && ! class_exists($model))) {
return null;
}

if (! is_object($model)) {
$model = new $model;
}
Expand Down
15 changes: 14 additions & 1 deletion tests/Feature/HasPermalinks/NestingTest.php
Expand Up @@ -29,4 +29,17 @@ public function it_will_have_unique_parent_for_records()
Permalink::create(['slug' => 'foo', 'parent_for' => User::class]);
Permalink::create(['slug' => 'foo', 'parent_for' => User::class]);
}
}

/** @test */
public function it_wont_nest_permalink_if_disabled()
{
factory(User::class)->create();
config()->set('permalink.nest_to_parent_on_create', false);

$parent = Permalink::create(['slug' => 'foo', 'parent_for' => User::class]);
$child = Permalink::create(['slug' => 'bar', 'entity_type' => User::class, 'entity_id' => 1]);

$this->assertNull($child->parent);
$this->assertCount(0, $parent->children);
}
}
Expand Up @@ -2,10 +2,10 @@

use Devio\Permalink\Permalink;
use Devio\Permalink\Tests\TestCase;
use Devio\Permalink\Services\NestingService;
use Devio\Permalink\Services\PathBuilder;
use Devio\Permalink\Tests\Support\Models\User;

class NestingServiceTest extends TestCase
class PathBuilderTest extends TestCase
{
/** @test */
public function it_generates_parent_path()
Expand All @@ -15,7 +15,7 @@ public function it_generates_parent_path()
'parent_for' => User::class
]);

$slugs = NestingService::parentPath(User::class);
$slugs = PathBuilder::parentPath(User::class);

$this->assertEquals(['users'], $slugs);
}
Expand All @@ -28,7 +28,7 @@ public function it_can_generate_parent_path_with_object_instance()
'parent_for' => User::class
]);

$slugs = NestingService::parentPath(new User);
$slugs = PathBuilder::parentPath(new User);

$this->assertEquals(['users'], $slugs);
}
Expand All @@ -47,7 +47,7 @@ public function it_nest_paths_recursively()
]);


$slugs = NestingService::parentPath(new User);
$slugs = PathBuilder::parentPath(new User);

$this->assertEquals(['account', 'users'], $slugs);
}
Expand Down

0 comments on commit cba8b60

Please sign in to comment.