1
0
cms11/app/Services/BundleCreators/BundleCreatorFactory.php

47 lines
1.2 KiB
PHP

<?php
namespace App\Services\BundleCreators;
use App\Exceptions\BundleCreatorCannotBeFound;
use Illuminate\Filesystem\FilesystemManager;
class BundleCreatorFactory
{
/**
* Registered bundle creators
*/
protected static $bundleCreators = [];
protected $filesystem;
public function __construct(FilesystemManager $filesystemManager)
{
$this->filesystem = $filesystemManager->disk(env('CONTENT_DISK'));
}
/**
* Return a bundle creator instance for specified section and with specified
* data, if available.
*/
public function getBundleCreatorFor(string $section, ?array $data = [])
{
foreach (self::$bundleCreators as $bundleCreator) {
if ($bundleCreator::handles($section, $data)) {
return $bundleCreator::make($data, $this->filesystem);
}
}
throw new BundleCreatorCannotBeFound();
}
/**
* Register a bundle creator
*/
public static function registerBundleCreator($bundleCreator)
{
if (!in_array($bundleCreator, self::$bundleCreators)) {
self::$bundleCreators[] = $bundleCreator;
}
}
}