Skip to content

Commit

Permalink
Merge branch '3.x' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed May 13, 2024
2 parents aca5202 + 2cc360d commit cd5835b
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/Database/Relations/AttachMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public function __construct(Builder $query, Model $parent, $type, $id, $isPublic
*/
public function setSimpleValue($value)
{
// Nulling the relationship
if (!$value) {
$this->parent->unsetRelation($this->relationName);

if ($this->parent->exists) {
$this->parent->bindEventOnce('model.afterSave', function() {
$this->ensureRelationIsEmpty();
});
}
return;
}

// Append a single newly uploaded file(s)
if ($value instanceof UploadedFile) {
$this->parent->bindEventOnce('model.afterSave', function () use ($value) {
Expand Down
16 changes: 16 additions & 0 deletions src/Database/Relations/AttachOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public function setSimpleValue($value)
$value = reset($value);
}

// Nulling the relationship
if (!$value) {
$this->parent->setRelation($this->relationName, null);

if ($this->parent->exists) {
$this->parent->bindEventOnce('model.afterSave', function() {
$this->ensureRelationIsEmpty();
});
}
return;
}

// Newly uploaded file
if ($value instanceof UploadedFile) {
$this->parent->bindEventOnce('model.afterSave', function () use ($value) {
Expand All @@ -67,6 +79,10 @@ public function setSimpleValue($value)

// The relation is set here to satisfy validation
$this->parent->setRelation($this->relationName, $value);

$this->parent->bindEventOnce('model.afterValidate', function() {
$this->parent->unsetRelation($this->relationName);
});
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Database/Relations/AttachOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ public function makeValidationFile($value)
return $value;
}

/**
* ensureRelationIsEmpty ensures the relation is empty, either deleted or nulled.
*/
protected function ensureRelationIsEmpty()
{
$options = $this->parent->getRelationDefinition($this->relationName);

if (array_get($options, 'delete', false)) {
$this->delete();
}
else {
$this->update([$this->getForeignKeyName() => null]);
}
}

/**
* getRelatedKeyName
* @return string
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function setSimpleValue($value)
{
// Nulling the relationship
if (!$value) {
$this->parent->unsetRelation($this->relationName);

if ($this->parent->exists) {
$this->parent->bindEventOnce('model.afterSave', function() {
$this->ensureRelationIsEmpty();
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function setSimpleValue($value)

// Nulling the relationship
if (!$value) {
$this->parent->setRelation($this->relationName, null);

if ($this->parent->exists) {
$this->parent->bindEventOnce('model.afterSave', function() {
$this->ensureRelationIsEmpty();
Expand Down
30 changes: 25 additions & 5 deletions src/Database/Scopes/MultisiteScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MultisiteScope implements ScopeInterface
/**
* @var array extensions to be added to the builder.
*/
protected $extensions = ['WithSite', 'WithSites'];
protected $extensions = ['WithSite', 'WithSites', 'WithSyncSites'];

/**
* apply the scope to a given Eloquent query builder.
Expand All @@ -29,12 +29,15 @@ public function apply(BuilderBase $builder, ModelBase $model)
}

/**
* addWithSite
* addWithSite removes this scope and includes the specified site
*/
protected function addWithSite(BuilderBase $builder)
{
$builder->macro('withSite', function (BuilderBase $builder, $siteId) {
return $builder->where($builder->getModel()->getQualifiedSiteIdColumn(), $siteId);
return $builder
->withoutGlobalScope($this)
->where($builder->getModel()->getQualifiedSiteIdColumn(), $siteId)
;
});
}

Expand All @@ -43,8 +46,25 @@ protected function addWithSite(BuilderBase $builder)
*/
protected function addWithSites(BuilderBase $builder)
{
$builder->macro('withSites', function (BuilderBase $builder) {
return $builder->withoutGlobalScope($this);
$builder->macro('withSites', function (BuilderBase $builder, $siteIds = null) {
if (!is_array($siteIds)) {
return $builder->withoutGlobalScope($this);
}

return $builder
->withoutGlobalScope($this)
->whereIn($builder->getModel()->getQualifiedSiteIdColumn(), $siteIds)
;
});
}

/**
* addWithSyncSites removes this scope and includes sites that should be synced with this model
*/
protected function addWithSyncSites(BuilderBase $builder)
{
$builder->macro('withSyncSites', function (BuilderBase $builder) {
return $builder->withSites($builder->getModel()->getMultisiteSyncSites());
});
}

Expand Down
24 changes: 19 additions & 5 deletions src/Database/Traits/Multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ public function multisiteSaveComplete()

Site::withGlobalContext(function() {
$otherModels = $this->newOtherSiteQuery()->get();
$otherSites = $otherModels->pluck('site_id')->all();
$otherSites = $this->getMultisiteSyncSites();

// Propagate attributes to known records
if ($this->propagatable) {
foreach ($otherModels as $model) {
$this->propagateToSite($model->site_id, $model);
foreach ($otherSites as $siteId) {
if ($model = $otherModels->where('site_id', $siteId)->first()) {
$this->propagateToSite($siteId, $model);
}
}
}

// Sync non-existent records
if ($this->isMultisiteSyncEnabled()) {
$missingSites = array_diff($this->getMultisiteSyncSites(), $otherSites);
$missingSites = array_diff($otherSites, $otherModels->pluck('site_id')->all());
foreach ($missingSites as $missingSite) {
$this->propagateToSite($missingSite);
}
Expand Down Expand Up @@ -280,6 +282,18 @@ public function propagateToSite($siteId, $otherModel = null)
return $otherModel;
}

/**
* getMultisiteKey returns the root key if multisite is used
*/
public function getMultisiteKey()
{
if (!$this->isMultisiteEnabled()) {
return $this->getKey();
}

return $this->site_root_id ?: $this->getKey();
}

/**
* isMultisiteEnabled allows for programmatic toggling
* @return bool
Expand Down Expand Up @@ -385,7 +399,7 @@ public function findOrCreateForSite($siteId = null)

// Newly created model
if (!$otherModel->exists) {
$otherModel->save();
$otherModel->save(['force' => true]);
}

// Restoring a trashed model
Expand Down
8 changes: 4 additions & 4 deletions src/Extension/ExtendableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ trait ExtendableTrait
public function extendableConstruct()
{
// Apply init callbacks
$classes = array_merge([static::class], class_parents($this));
$classes = array_merge([static::class], class_parents(static::class));
foreach ($classes as $class) {
if (isset(Container::$classCallbacks[$class]) && is_array(Container::$classCallbacks[$class])) {
foreach (Container::$classCallbacks[$class] as $callback) {
Expand Down Expand Up @@ -435,7 +435,7 @@ public function extendableGet($name)
}
}

$parent = get_parent_class();
$parent = get_parent_class(self::class);
if ($parent !== false && method_exists($parent, '__get')) {
return parent::__get($name);
}
Expand Down Expand Up @@ -468,7 +468,7 @@ public function extendableSet($name, $value)
}

// This targets trait usage in particular
$parent = get_parent_class();
$parent = get_parent_class(self::class);
if ($parent !== false && method_exists($parent, '__set')) {
parent::__set($name, $value);
$found = true;
Expand Down Expand Up @@ -504,7 +504,7 @@ public function extendableCall($name, $params = null)
return call_user_func_array($callable, $params);
}

$parent = get_parent_class();
$parent = get_parent_class(self::class);
if ($parent !== false && method_exists($parent, '__call')) {
return parent::__call($name, $params);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace October\Rain\Filesystem;

use Event;
use ReflectionClass;
use FilesystemIterator;
use Illuminate\Filesystem\Filesystem as FilesystemBase;
Expand Down Expand Up @@ -103,6 +104,20 @@ public function sizeToString($bytes)
*/
public function localToPublic($path)
{
/**
* @event filesystem.localToPublic
* Allow custom logic for converting local to public paths on non-standard installations.
*
* Example usage
*
* Event::listen('filesystem.localToPublic', function ($path) {
* return '/custom/public/path';
* });
*/
if (($event = Event::fire('filesystem.localToPublic', [$path], true)) !== null) {
return $event;
}

// Check real paths
$basePath = base_path();
if (strpos($path, $basePath) === 0) {
Expand Down
12 changes: 6 additions & 6 deletions src/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class Kernel extends HttpKernel
* @var array
*/
protected $routeMiddleware = [
// 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
// 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
// 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
// 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
Expand Down Expand Up @@ -79,9 +79,9 @@ class Kernel extends HttpKernel
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
// \Illuminate\Auth\Middleware\Authenticate::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Auth\Middleware\Authenticate::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
// \Illuminate\Auth\Middleware\Authorize::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
}
2 changes: 1 addition & 1 deletion src/Html/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Helper
*/
public static function nameToId($string)
{
return rtrim(str_replace('--', '-', str_replace(['[', ']'], '-', $string)), '-');
return rtrim(str_replace('--', '-', str_replace(['[', ']', ':', '.'], '-', $string)), '-');
}

/**
Expand Down
17 changes: 16 additions & 1 deletion src/Support/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ClassLoader
*/
public $manifest = [];

/**
* @var array unknownClasses cache
*/
protected $unknownClasses = [];

/**
* @var bool manifestDirty if manifest needs to be written
*/
Expand Down Expand Up @@ -68,6 +73,10 @@ public function __construct(Filesystem $files, string $basePath)
*/
public function load($class): bool
{
if (!str_contains($class, '\\')) {
return false;
}

if (
isset($this->manifest[$class]) &&
is_file($fullPath = $this->basePath.DIRECTORY_SEPARATOR.$this->manifest[$class])
Expand All @@ -76,6 +85,10 @@ public function load($class): bool
return true;
}

if (isset($this->unknownClasses[$class])) {
return false;
}

[$lowerClass, $upperClass] = $this->normalizeClass($class);

// Load namespaces
Expand All @@ -94,6 +107,8 @@ public function load($class): bool
}
}

$this->unknownClasses[$class] = true;

return false;
}

Expand Down Expand Up @@ -150,7 +165,7 @@ public function register(): void

$this->registered = spl_autoload_register(function($class) {
$this->load($class);
}, true, true);
});
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Support/Facades/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
* @method static bool hasAnySite()
* @method static bool hasMultiSite()
* @method static bool hasSiteGroups()
* @method static array listEnabled()
* @method static \System\Classes\SiteCollection listEnabled()
* @method static \System\Classes\SiteCollection listSites()
* @method static array listSiteIds()
* @method static iterable listSites()
* @method static array listSiteIdsInGroup($siteId)
* @method static array listSiteIdsInLocale($siteId)
* @method static mixed getEditSite()
* @method static string getEditSiteId()
* @method static void setEditSiteId(string $siteId)
* @method static void setEditSite(mixed $site)
* @method static mixed getAnyEditSite()
* @method static bool hasAnyEditSite()
* @method static bool hasMultiEditSite()
* @method static iterable listEditEnabled()
* @method static \System\Classes\SiteCollection listEditEnabled()
* @method static void applyEditSite(mixed $site)
* @method static mixed getActiveSite()
* @method static string getActiveSiteId()
* @method static void setActiveSiteId(string $siteId)
* @method static void setActiveSite(mixed $site)
* @method static void applyActiveSite(mixed $site)
* @method static array listSiteIdsInGroup($siteId)
* @method static array listSiteIdsInLocale($siteId)
* @method static int|null getSiteIdFromContext()
* @method static string|null getSiteCodeFromContext()
* @method static mixed getSiteFromContext()
Expand Down

0 comments on commit cd5835b

Please sign in to comment.