-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add registry, rename mongodb storage to storage. upd readme.
- Loading branch information
Showing
5 changed files
with
114 additions
and
141 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
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,75 @@ | ||
<?php | ||
namespace Makasim\Yadm; | ||
|
||
class Registry | ||
{ | ||
/** | ||
* @var array|Storage[] | ||
*/ | ||
private $storages; | ||
|
||
/** | ||
* @var array|Repository[] | ||
*/ | ||
private $repositories; | ||
|
||
/** | ||
* @param Storage[]|array $storages | ||
* @param Repository[]|array $repositories | ||
*/ | ||
public function __construct(array $storages, array $repositories) | ||
{ | ||
$this->storages = $storages; | ||
$this->repositories = $repositories; | ||
} | ||
|
||
/** | ||
* @param object|string $modelOrClass | ||
* | ||
* @return Storage | ||
*/ | ||
public function getStorage($modelOrClass) | ||
{ | ||
$class = is_object($modelOrClass) ? get_class($modelOrClass) : $modelOrClass; | ||
|
||
if (false == array_key_exists($class, $this->storages)) { | ||
throw new \InvalidArgumentException(sprintf('The storage for model "%s" does not exist', $class)); | ||
} | ||
|
||
$storage = $this->storages[$class]; | ||
if (false == $storage instanceof Storage) { | ||
throw new \LogicException(sprintf( | ||
'Storage must be instance of %s but got %s', | ||
Storage::class, | ||
is_object($storage) ? get_class($storage) : gettype($storage) | ||
)); | ||
} | ||
|
||
return $storage; | ||
} | ||
|
||
/** | ||
* @param object|string $modelOrClass | ||
* | ||
* @return Repository | ||
*/ | ||
public function getRepository($modelOrClass) | ||
{ | ||
$class = is_object($modelOrClass) ? get_class($modelOrClass) : $modelOrClass; | ||
|
||
if (false == array_key_exists($class, $this->repositories)) { | ||
$this->repositories = new Repository($this->getStorage($class)); | ||
} | ||
|
||
$repository = $this->repositories[$class]; | ||
if (false == $repository instanceof Repository) { | ||
throw new \LogicException(sprintf( | ||
'Repository must be instance of %s but got %s', | ||
Repository::class, | ||
is_object($repository) ? get_class($repository) : gettype($repository) | ||
)); | ||
} | ||
|
||
return $repository; | ||
} | ||
} |
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
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