-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Extendable component data object
- Loading branch information
Showing
10 changed files
with
291 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SMS\FluidComponents\Domain\Model; | ||
|
||
class Component | ||
{ | ||
protected string $namespace; | ||
protected string $package; | ||
protected string $name; | ||
protected string $file; | ||
|
||
protected array $data = []; | ||
protected ?string $class = null; | ||
protected ?string $prefix = null; | ||
|
||
public function __construct( | ||
string $fullNamespace, | ||
string $package, | ||
string $name, | ||
string $file | ||
) { | ||
$this->namespace = $fullNamespace; | ||
$this->package = $package; | ||
$this->name = $name; | ||
$this->file = $file; | ||
} | ||
|
||
public function getNamespace(): string | ||
{ | ||
return $this->namespace; | ||
} | ||
|
||
public function getPackage(): string | ||
{ | ||
return $this->package; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getPath(): string | ||
{ | ||
return dirname($this->file); | ||
} | ||
|
||
public function getFile(): string | ||
{ | ||
return $this->file; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function setData(array $data): void | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
public function getClass(): ?string | ||
{ | ||
return $this->class; | ||
} | ||
|
||
public function setClass(string $class): void | ||
{ | ||
$this->class = $class; | ||
} | ||
|
||
public function getPrefix(): ?string | ||
{ | ||
return $this->prefix; | ||
} | ||
|
||
public function setPrefix(string $prefix): void | ||
{ | ||
$this->prefix = $prefix; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace SMS\FluidComponents\Interfaces; | ||
|
||
use SMS\FluidComponents\Domain\Model\Component; | ||
|
||
interface ComponentDataProvider | ||
{ | ||
public function applyData(Component $component); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SMS\FluidComponents\Service; | ||
|
||
use SMS\FluidComponents\Domain\Model\Component; | ||
use TYPO3\CMS\Core\SingletonInterface; | ||
|
||
class ComponentDataLoader implements SingletonInterface | ||
{ | ||
private array $loadedComponents = []; | ||
private iterable $dataProviders; | ||
|
||
public function __construct(iterable $dataProviders) | ||
{ | ||
$this->dataProviders = $dataProviders; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->loadedComponents = []; | ||
} | ||
|
||
public function loadData(Component $component): void | ||
{ | ||
if (isset($this->loadedComponents[$component->getNamespace()])) { | ||
return; | ||
} | ||
|
||
foreach ($this->dataProviders as $dataProvider) { | ||
$dataProvider->applyData($component); | ||
} | ||
$this->loadedComponents[$component->getNamespace()] = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SMS\FluidComponents\Service\DataProvider; | ||
|
||
use SMS\FluidComponents\Domain\Model\Component; | ||
use SMS\FluidComponents\Interfaces\ComponentDataProvider; | ||
|
||
class ComponentPrefixer implements ComponentDataProvider | ||
{ | ||
public function applyData(Component $component): void | ||
{ | ||
if ($component->getPrefix() !== null) { | ||
return; | ||
} | ||
|
||
$vendorName = substr($component->getPackage(), 0, strpos($component->getPackage(), '\\')); | ||
$componentName = str_replace('\\', '', $component->getName()); | ||
$prefix = strtolower($vendorName) . $componentName; | ||
|
||
$component->setClass($prefix); | ||
$component->setPrefix($prefix . '_'); | ||
} | ||
} |
Oops, something went wrong.