Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php declare(strict_types = 1);
- namespace NameSpace\Units\Core\Registers;
- use NameSpace\Units\Core\Registers\Interfaces\Registrable;
- use NameSpace\Support\Env;
- use NameSpace\Support\Traits\SingletonTrait;
- use NameSpace\Units\Core\Registers\Traits\EnforceEnvRegistration;
- class AbstractRegister
- {
- use SingletonTrait;
- use EnforceEnvRegistration;
- protected string $bufferIdentity = '';
- protected array $registers = [];
- /**
- * add
- *
- * @param mixed $tag
- * @param mixed $callback
- * @param mixed $priority
- * @param mixed $argNums
- *
- * @return self
- */
- public function add(Registrable $registry): self
- {
- $this->_register($registry);
- return $this;
- }
- /**
- * register
- *
- * @param mixed $registry
- *
- * @return Registrable
- */
- public function _register(Registrable $registry): void
- {
- $this->generatetBufferIdentity();
- $this->registers[$this->getBufferIdentity()] = $registry;
- }
- /**
- * generatetBufferIdentity
- *
- * generate an unique identifier
- * to serve as buffer registration id
- *
- * @param mixed $id
- *
- * @return void
- */
- protected function generatetBufferIdentity(): void
- {
- $this->bufferIdentity = uniqid((string) time());
- }
- /**
- * getBufferIdentity
- *
- * @return string
- */
- protected function getBufferIdentity(): string
- {
- return $this->bufferIdentity;
- }
- /**
- * init
- *
- * @return void
- */
- public function init(): void
- {
- foreach ($this->getRegistered() as $registry)
- {
- call_user_func(static::REGISTER_FUNCTION, ...$registry->toArray());
- }
- }
- /**
- * getRegistered
- *
- * @return array
- */
- public function getRegistered(): array
- {
- return $this->registers;
- }
- /**
- * env
- *
- * check wheater it registers of not
- * depending on the environment
- *
- * @return void
- */
- public function env(string $env): void
- {
- if (Env::instance()->is($env))
- {
- return;
- }
- if ($env === Env::ALL)
- {
- return;
- }
- unset($this->getRegistered()[$this->getBufferIdentity()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment