Advertisement
kwasinski

EnforceEnvRegistration

May 11th, 2023
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | Software | 0 0
  1. <?php declare(strict_types = 1);
  2.  
  3. namespace NameSpace\Units\Core\Registers\Traits;
  4.  
  5. use NameSpace\Support\Exceptions\AppException;
  6. use NameSpace\Units\Core\Registers\Interfaces\Registrable;
  7.  
  8. trait EnforceEnvRegistration
  9. {
  10.     protected bool $hasRegistered = false;
  11.  
  12.     protected bool $hasEnvSet = false;
  13.  
  14.     /**
  15.      * add
  16.      *
  17.      * @param  mixed $registry
  18.      * @return self
  19.      */
  20.     public function add(Registrable $registry): self
  21.     {
  22.         $this->hasRegistered = true;
  23.         parent::add($registry);
  24.     }
  25.  
  26.     /**
  27.      * env
  28.      *
  29.      * @param  mixed $env
  30.      * @return void
  31.      */
  32.     public function env(string $env): void
  33.     {
  34.         $this->hasEnvSet = true;
  35.  
  36.         parent::env($env);
  37.     }
  38.  
  39.     /**
  40.      * checkEnvEnforcement
  41.      *
  42.      * @return void
  43.      */
  44.     public function checkEnvEnforcement(): void
  45.     {
  46.         if (!$this->hasRegistered && $this->hasEnvSet)
  47.         {
  48.             throw new AppException('please call ->env() after calling ->add() method from the registers');
  49.         }
  50.     }
  51.  
  52.  
  53. }
  54.  
Tags: php wordpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement