"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Application.php" (13 Dec 2019, 8978 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Application.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 6.9.0_vs_6.9.1.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya {
   16 
   17   use Papaya\Application\Profile;
   18   use Papaya\Application\Profiles;
   19   use Papaya\BaseObject\Interfaces\Properties;
   20 
   21   /**
   22    * Papaya Application - object registry with profiles
   23    *
   24    * @package Papaya-Library
   25    * @subpackage Application
   26    */
   27   class Application implements \ArrayAccess, Properties, \IteratorAggregate {
   28     /**
   29      * Duplicate profiles trigger an error
   30      *
   31      * @var int
   32      */
   33     const DUPLICATE_ERROR = 0;
   34 
   35     /**
   36      * Ignore duplicate profiles
   37      *
   38      * @var int
   39      */
   40     const DUPLICATE_IGNORE = 1;
   41 
   42     /**
   43      * Overwrite duplicate profiles
   44      *
   45      * @var int
   46      */
   47     const DUPLICATE_OVERWRITE = 2;
   48 
   49     /**
   50      * Class variable for singleton instance
   51      *
   52      * @var Application
   53      */
   54     private static $instance;
   55 
   56     /**
   57      * Profile objects
   58      *
   59      * @var array
   60      */
   61     private $_profiles = [];
   62 
   63     /**
   64      * Objects
   65      *
   66      * @var array(object)
   67      */
   68     private $_objects = [];
   69 
   70     /**
   71      * Create a new instance of this class or return existing one (singleton)
   72      *
   73      * @param bool $reset
   74      * @return self
   75      */
   76     public static function getInstance($reset = FALSE) {
   77       if ($reset || NULL === self::$instance) {
   78         self::$instance = new self();
   79       }
   80       return self::$instance;
   81     }
   82 
   83     /**
   84      * @return \Traversable
   85      */
   86     public function getIterator() {
   87       return new \ArrayIterator($this->_objects);
   88     }
   89 
   90     /**
   91      * @return array
   92      */
   93     public function getProfiles() {
   94       return $this->_profiles;
   95     }
   96 
   97     /**
   98      * Register a collection of profiles
   99      *
  100      * @param Profiles $profiles
  101      * @param int $duplicationMode
  102      */
  103     public function registerProfiles(
  104       Application\Profiles $profiles, $duplicationMode = self::DUPLICATE_ERROR
  105     ) {
  106       foreach ($profiles->getProfiles($this) as $identifier => $profile) {
  107         $this->registerProfile($identifier, $profile, $duplicationMode);
  108       }
  109     }
  110 
  111     /**
  112      * Register an object profile
  113      *
  114      * @param string $identifier
  115      * @param Profile|callable $profile
  116      * @param int $duplicationMode
  117      *
  118      * @throws \InvalidArgumentException
  119      */
  120     public function registerProfile(
  121       $identifier, $profile, $duplicationMode = self::DUPLICATE_ERROR
  122     ) {
  123       if (!($profile instanceof Application\Profile || \is_callable($profile))) {
  124         throw new \InvalidArgumentException(
  125           \sprintf(
  126             'Invalid profile %s is %s.',
  127             $identifier,
  128             \is_object($profile) ? \get_class($profile) : \gettype($profile)
  129           )
  130         );
  131       }
  132       $index = \strtolower($identifier);
  133       if (isset($this->_profiles[$index])) {
  134         switch ($duplicationMode) {
  135         case self::DUPLICATE_OVERWRITE :
  136           break;
  137         case self::DUPLICATE_ERROR :
  138           throw new \InvalidArgumentException(
  139             \sprintf(
  140               'Duplicate application object profile: "%s"',
  141               $identifier
  142             )
  143           );
  144         case self::DUPLICATE_IGNORE :
  145           return;
  146         }
  147       }
  148       if (!empty($index)) {
  149         $this->_profiles[$index] = $profile;
  150       }
  151     }
  152 
  153     /**
  154      * Get object instance, if the object does not exist and no profile is found, $className is
  155      * used to create a new object, if provided.
  156      *
  157      * @param string $identifier
  158      * @param bool $silent
  159      * @return object
  160      */
  161     public function getObject($identifier, $silent = FALSE) {
  162       $index = \strtolower($identifier);
  163       if (
  164         isset($this->_objects[$index]) &&
  165         \is_object($this->_objects[$index])
  166       ) {
  167         return $this->_objects[$index];
  168       }
  169       if (isset($this->_profiles[$index])) {
  170         $profile = $this->_profiles[$index];
  171         if ($profile instanceof Application\Profile) {
  172           return $this->_objects[$index] = $profile->createObject($this);
  173         }
  174         return $this->_objects[$index] = $profile($this);
  175       }
  176       if ($silent) {
  177         return NULL;
  178       }
  179       throw new \InvalidArgumentException(
  180         'Unknown profile identifier: '.$identifier
  181       );
  182     }
  183 
  184     /**
  185      * Store an object in the application registry.
  186      *
  187      * @param string $identifier
  188      * @param object $object
  189      * @param int $duplicationMode
  190      *
  191      * @throws \LogicException
  192      */
  193     public function setObject($identifier, $object, $duplicationMode = self::DUPLICATE_ERROR) {
  194       Utility\Constraints::assertObject($object);
  195       $index = \strtolower($identifier);
  196       if (isset($this->_objects[$index])) {
  197         switch ($duplicationMode) {
  198         case self::DUPLICATE_OVERWRITE :
  199           break;
  200         case self::DUPLICATE_ERROR :
  201           throw new \LogicException(
  202             \sprintf(
  203               'Application object does already exists: "%s"',
  204               $identifier
  205             )
  206           );
  207         case self::DUPLICATE_IGNORE :
  208           return;
  209         }
  210       }
  211       $this->_objects[$index] = $object;
  212     }
  213 
  214     /**
  215      * Check if an object or an profile for an object exists
  216      *
  217      * @param string $identifier
  218      * @param bool $checkProfiles
  219      *
  220      * @return bool
  221      */
  222     public function hasObject($identifier, $checkProfiles = TRUE) {
  223       $index = \strtolower($identifier);
  224       if (
  225         isset($this->_objects[$index]) &&
  226         \is_object($this->_objects[$index])
  227       ) {
  228         return TRUE;
  229       }
  230       if (!$checkProfiles) {
  231         return FALSE;
  232       }
  233       return isset($this->_profiles[$index]);
  234     }
  235 
  236     /**
  237      * Remove and existing object and optionally its profile
  238      *
  239      * @param string $identifier
  240      *
  241      * @param bool $removeProfile
  242      */
  243     public function removeObject($identifier, $removeProfile = FALSE) {
  244       if ($this->hasObject($identifier)) {
  245         $index = \strtolower($identifier);
  246         if (
  247         isset($this->_objects[$index])
  248         ) {
  249           unset($this->_objects[$index]);
  250         }
  251         if ($removeProfile && isset($this->_profiles[$index])) {
  252           unset($this->_profiles[$index]);
  253         }
  254       } else {
  255         throw new \InvalidArgumentException(
  256           'Unknown profile identifier: '.$identifier
  257         );
  258       }
  259     }
  260 
  261     /**
  262      * Allow property syntax to check object are available, this will return true even if only
  263      * a profile for the object exists.
  264      *
  265      * @param string $name
  266      *
  267      * @return bool
  268      * @see setObject
  269      *
  270      */
  271     public function __isset($name) {
  272       return $this->hasObject($name);
  273     }
  274 
  275     /**
  276      * Allow property syntax to get objects from the registry.
  277      *
  278      * @param string $name
  279      *
  280      * @return object
  281      * @see getObject
  282      *
  283      */
  284     public function __get($name) {
  285       return $this->getObject($name, TRUE);
  286     }
  287 
  288     /**
  289      * Allow property syntax to put objects into the registry.
  290      *
  291      * @param string $name
  292      * @param object $value
  293      * @see setObject
  294      *
  295      */
  296     public function __set($name, $value) {
  297       $this->setObject($name, $value);
  298     }
  299 
  300     /**
  301      * Allow property syntax to remove objects from the registry.
  302      *
  303      * @param string $name
  304      * @see setObject
  305      *
  306      */
  307     public function __unset($name) {
  308       $this->removeObject($name);
  309     }
  310 
  311     /**
  312      * Allow method syntax to get/set objects from/into the registry.
  313      *
  314      * @param string $name
  315      * @param $arguments
  316      *
  317      * @return object
  318      * @see __set
  319      *
  320      * @see __get
  321      */
  322     public function __call($name, $arguments) {
  323       if (isset($arguments[0])) {
  324         $this->__set($name, $arguments[0]);
  325       }
  326       return $this->__get($name);
  327     }
  328 
  329     /**
  330      * @param string $offset
  331      *
  332      * @return bool
  333      */
  334     public function offsetExists($offset) {
  335       return $this->hasObject($offset);
  336     }
  337 
  338     /**
  339      * @param string $offset
  340      *
  341      * @return object
  342      */
  343     public function offsetGet($offset) {
  344       return $this->getObject($offset);
  345     }
  346 
  347     /**
  348      * @param string $offset
  349      * @param object $value
  350      */
  351     public function offsetSet($offset, $value) {
  352       $this->setObject($offset, $value);
  353     }
  354 
  355     /**
  356      * @param string $offset
  357      */
  358     public function offsetUnset($offset) {
  359       $this->removeObject($offset);
  360     }
  361   }
  362 }