Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Nov 1, 2023
1 parent fa64c2e commit 1c0132e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/app/Library/Uploaders/Support/RegisterUploadEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Backpack\CRUD\app\Library\CrudPanel\CrudField;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
use Backpack\CRUD\app\Library\Uploaders\Support\UploaderLocator;
use Exception;
use Illuminate\Database\Eloquent\Relations\Pivot;

Expand Down Expand Up @@ -45,19 +46,21 @@ private function registerEvents(array|null $subfield = [], ?bool $registerModelE

$attributes = $this->crudObject->getAttributes();
$model = $attributes['model'] ?? get_class($this->crudObject->crud()->getModel());
$uploader = $this->getUploader($attributes, $this->uploaderConfiguration);
/** @var UploaderInterface */
$uploader = UploaderLocator::for($attributes, $this->uploaderConfiguration, $this->crudObjectType, $this->macro);

if (isset($attributes['relation_type']) && $attributes['entity'] !== false) {
$uploader = $uploader->relationship(true);
}

$this->setupModelEvents($model, $uploader);
$this->setupUploadConfigsInCrudObject($uploader);
}

private function registerSubfieldEvent(array $subfield, bool $registerModelEvents = true): void
{
$uploader = $this->getUploader($subfield, $this->uploaderConfiguration);
/** @var UploaderInterface */
$uploader = UploaderLocator::for($subfield, $this->uploaderConfiguration, $this->crudObjectType, $this->macro);
$crudObject = $this->crudObject->getAttributes();
$uploader = $uploader->repeats($crudObject['name']);

Expand Down
21 changes: 21 additions & 0 deletions src/app/Library/Uploaders/Support/UploaderLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Backpack\CRUD\app\Library\Uploaders\Support;
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
use Exception;

final class UploaderLocator
{
public static function for(array $crudObject, array $uploaderConfiguration, string $crudObjectType, string $macro): UploaderInterface
{
if (isset($uploaderConfiguration['uploader']) && class_exists($uploaderConfiguration['uploader'])) {
return $uploaderConfiguration['uploader']::for($crudObject, $uploaderConfiguration);
}

if (app('UploadersRepository')->hasUploadFor($crudObject['type'], $macro)) {
return app('UploadersRepository')->getUploadFor($crudObject['type'], $macro)::for($crudObject, $uploaderConfiguration);
}

throw new Exception('Undefined upload type for '.$crudObjectType.' type: '.$crudObject['type']);
}
}
6 changes: 2 additions & 4 deletions src/app/Library/Uploaders/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace Backpack\CRUD\app\Library\Uploaders;

use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
use Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleFileNaming;
use Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleRepeatableUploads;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

abstract class Uploader implements UploaderInterface
{
use HandleFileNaming;
use HandleRepeatableUploads;
use \Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleFileNaming,
\Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleRepeatableUploads;

private string $name;

Expand Down

0 comments on commit 1c0132e

Please sign in to comment.