kwasinski

AbstractRegister.php

May 11th, 2023
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | Software | 0 0
  1. <?php declare(strict_types = 1);
  2.  
  3. namespace NameSpace\Units\Core\Registers;
  4.  
  5. use NameSpace\Units\Core\Registers\Interfaces\Registrable;
  6.  
  7. use NameSpace\Support\Env;
  8. use NameSpace\Support\Traits\SingletonTrait;
  9. use NameSpace\Units\Core\Registers\Traits\EnforceEnvRegistration;
  10.  
  11. class AbstractRegister
  12. {
  13.     use SingletonTrait;
  14.     use EnforceEnvRegistration;
  15.  
  16.     protected string $bufferIdentity = '';
  17.  
  18.     protected array $registers = [];
  19.  
  20.     /**
  21.      * add
  22.      *
  23.      * @param  mixed $tag
  24.      * @param  mixed $callback
  25.      * @param  mixed $priority
  26.      * @param  mixed $argNums
  27.      *
  28.      * @return self
  29.      */
  30.     public function add(Registrable $registry): self
  31.     {
  32.         $this->_register($registry);
  33.  
  34.         return $this;
  35.     }
  36.  
  37.     /**
  38.      * register
  39.      *
  40.      * @param  mixed $registry
  41.      *
  42.      * @return Registrable
  43.      */
  44.     public function _register(Registrable $registry): void
  45.     {
  46.         $this->generatetBufferIdentity();
  47.  
  48.         $this->registers[$this->getBufferIdentity()] = $registry;
  49.     }
  50.  
  51.     /**
  52.      * generatetBufferIdentity
  53.      *
  54.      * generate an unique identifier
  55.      * to serve as buffer registration id
  56.      *
  57.      * @param  mixed $id
  58.      *
  59.      * @return void
  60.      */
  61.     protected function generatetBufferIdentity(): void
  62.     {
  63.         $this->bufferIdentity = uniqid((string) time());
  64.     }
  65.  
  66.     /**
  67.      * getBufferIdentity
  68.      *
  69.      * @return string
  70.      */
  71.     protected function getBufferIdentity(): string
  72.     {
  73.         return $this->bufferIdentity;
  74.     }
  75.  
  76.     /**
  77.      * init
  78.      *
  79.      * @return void
  80.      */
  81.     public function init(): void
  82.     {
  83.         foreach ($this->getRegistered() as $registry)
  84.         {
  85.             call_user_func(static::REGISTER_FUNCTION, ...$registry->toArray());
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * getRegistered
  91.      *
  92.      * @return array
  93.      */
  94.     public function getRegistered(): array
  95.     {
  96.         return $this->registers;
  97.     }
  98.  
  99.     /**
  100.      * env
  101.      *
  102.      * check wheater it registers of not
  103.      * depending on the environment
  104.      *
  105.      * @return void
  106.      */
  107.     public function env(string $env): void
  108.     {
  109.         if (Env::instance()->is($env))
  110.         {
  111.             return;
  112.         }
  113.  
  114.         if ($env === Env::ALL)
  115.         {
  116.             return;
  117.         }
  118.  
  119.         unset($this->getRegistered()[$this->getBufferIdentity()]);
  120.     }
  121.  
  122. }
  123.  
Tags: php wordpress
Advertisement
Add Comment
Please, Sign In to add comment