API
This commit is contained in:
58
api/common/Factory/Model.php
Normal file
58
api/common/Factory/Model.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Factory;
|
||||
|
||||
use ProVM\Common\Implement\Repository;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class Model
|
||||
{
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->setContainer($container);
|
||||
}
|
||||
|
||||
protected ContainerInterface $container;
|
||||
protected array $repositories;
|
||||
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
public function getRepositories(): array
|
||||
{
|
||||
return $this->repositories;
|
||||
}
|
||||
public function getRepository(string $name): string
|
||||
{
|
||||
return $this->getRepositories()[$name];
|
||||
}
|
||||
|
||||
public function setContainer(ContainerInterface $container): Model
|
||||
{
|
||||
$this->container = $container;
|
||||
return $this;
|
||||
}
|
||||
public function addRepository(string $name, string $repository_class_name): Model
|
||||
{
|
||||
$this->repositories[$name] = $repository_class_name;
|
||||
return $this;
|
||||
}
|
||||
public function setRepositories(array $repositories): Model
|
||||
{
|
||||
foreach ($repositories as $name => $class) {
|
||||
$this->addRepository($name, $class);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function find(string $model_class_name): Repository
|
||||
{
|
||||
$name = str_replace("ProVM\\Emails\\Model\\", '', $model_class_name);
|
||||
try {
|
||||
$repository_class = $this->getRepository($name);
|
||||
} catch (\Exception $e) {
|
||||
$repository_class = str_replace('Model', 'Repository', $model_class_name);
|
||||
}
|
||||
return $this->getContainer()->get($repository_class);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user