Skip to content

Commit

Permalink
Merge pull request #31 from Stillat/multisite-entries
Browse files Browse the repository at this point in the history
Multisite entries
  • Loading branch information
JohnathonKoster authored Feb 11, 2024
2 parents e6b30e2 + a5edd9d commit 5f8f632
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 27 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^8.0",
"statamic/cms": "^4"
"statamic/cms": "^4.16"
},
"require-dev": {
"mockery/mockery": "^1.2.3",
Expand Down
42 changes: 16 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="APP_URL" value="http://localhost" />
<env name="APP_KEY" value="base64:O7iOsdFTYTIDqTzQJqDSJ/X4p03IW/QfL0Jqq+51ljI=" />
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="APP_URL" value="http://localhost"/>
<env name="APP_KEY" value="base64:O7iOsdFTYTIDqTzQJqDSJ/X4p03IW/QfL0Jqq+51ljI="/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
23 changes: 23 additions & 0 deletions src/Events/EventStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Stillat\Relationships\Events;

class EventStack
{
protected $count = 0;

public function increment()
{
$this->count++;
}

public function decrement()
{
$this->count--;
}

public function count()
{
return $this->count;
}
}
7 changes: 7 additions & 0 deletions src/Listeners/EntrySavedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Entries\Entry;
use Statamic\Events\EntrySaved;
use Stillat\Relationships\RelationshipManager;
use Stillat\Relationships\Support\Facades\EventStack;

class EntrySavedListener
{
Expand All @@ -20,6 +21,8 @@ public function __construct(RelationshipManager $manager)

public function handle(EntrySaved $event)
{
EventStack::decrement();

/** @var Entry $entry */
$entry = $event->entry;
$collection = $entry->collection();
Expand All @@ -28,6 +31,10 @@ public function handle(EntrySaved $event)
return;
}

if (EventStack::count() > 0) {
return;
}

$this->manager->processor()->setUpdatedEntryDetails($entry);

$handle = $collection->handle();
Expand Down
7 changes: 7 additions & 0 deletions src/Listeners/EntrySavingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Statamic\Entries\Entry;
use Statamic\Events\EntrySaving;
use Stillat\Relationships\RelationshipManager;
use Stillat\Relationships\Support\Facades\EventStack;

class EntrySavingListener extends BaseListener
{
Expand All @@ -27,6 +28,8 @@ public function __construct(RelationshipManager $manager, EntryRepository $entri

public function handle(EntrySaving $event)
{
EventStack::increment();

/** @var Entry $entry */
$entry = $event->entry;
$collection = $entry->collectionHandle();
Expand All @@ -35,6 +38,10 @@ public function handle(EntrySaving $event)
return;
}

if (EventStack::count() > 1) {
return;
}

$isUpdating = $entry->id() !== null;

if ($isUpdating) {
Expand Down
5 changes: 5 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Statamic\Providers\AddonServiceProvider;
use Stillat\Relationships\Console\Commands\FillRelationshipsCommand;
use Stillat\Relationships\Console\Commands\ListRelationshipsCommand;
use Stillat\Relationships\Events\EventStack;
use Stillat\Relationships\Listeners\EntryDeletedListener;
use Stillat\Relationships\Listeners\EntrySavedListener;
use Stillat\Relationships\Listeners\EntrySavingListener;
Expand Down Expand Up @@ -65,6 +66,10 @@ class ServiceProvider extends AddonServiceProvider

public function register()
{
$this->app->singleton(EventStack::class, function ($app) {
return new EventStack();
});

$this->app->singleton(RelationshipManager::class, function ($app) {
return new RelationshipManager($app->make(RelationshipProcessor::class));
});
Expand Down
19 changes: 19 additions & 0 deletions src/Support/Facades/EventStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Stillat\Relationships\Support\Facades;

use Illuminate\Support\Facades\Facade;
use Stillat\Relationships\Events\EventStack as EventStackConcrete;

/**
* @method static void increment()
* @method static void decrement()
* @method static int count()
*/
class EventStack extends Facade
{
public static function getFacadeAccessor()
{
return EventStackConcrete::class;
}
}
1 change: 1 addition & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('statamic.stache.stores.entries.directory', __DIR__.'/__fixtures__/content/collections');
$app['config']->set('statamic.stache.stores.navigation.directory', __DIR__.'/__fixtures__/content/navigation');
$app['config']->set('statamic.stache.stores.globals.directory', __DIR__.'/__fixtures__/content/globals');
$app['config']->set('statamic.stache.stores.global-variables.directory', __DIR__.'/__fixtures__/content/globals');
$app['config']->set('statamic.stache.stores.asset-containers.directory', __DIR__.'/__fixtures__/content/assets');
$app['config']->set('statamic.stache.stores.nav-trees.directory', __DIR__.'/__fixtures__/content/structures/navigation');
$app['config']->set('statamic.stache.stores.collection-trees.directory', __DIR__.'/__fixtures__/content/structures/collections');
Expand Down

0 comments on commit 5f8f632

Please sign in to comment.