Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • ActiveRecord
  • Cache
  • Configs
  • Core
  • DateTime
  • Debug
  • DeleteOperation
  • Errors
  • Event
  • EventHook
  • Events
  • FileCache
  • FormattedString
  • Helpers
  • I18n
  • Image
  • Inflections
  • Inflector
  • Models
  • Module
  • Modules
  • Object
  • Operation
  • PingOperation
  • Prototype
  • Route
  • Routes
  • SaveOperation
  • Session
  • TimeZone
  • TimeZoneLocation
  • Uploaded
  • Vars
  • VarsIterator

Interfaces

  • StorageInterface
  • ToArray
  • ToArrayRecursive

Traits

  • PrototypeTrait
  • ToArrayRecursiveTrait

Exceptions

  • AlreadyAuthenticated
  • AuthenticationRequired
  • Exception
  • ModuleConstructorMissing
  • ModuleIsDisabled
  • ModuleNotDefined
  • OffsetError
  • OffsetNotDefined
  • OffsetNotReadable
  • OffsetNotWritable
  • PermissionRequired
  • PropertyError
  • PropertyIsReserved
  • PropertyNotDefined
  • PropertyNotReadable
  • PropertyNotWritable
  • RouteNotDefined
  • SecurityException

Constants

  • TOKEN_ALPHA
  • TOKEN_ALPHA_UPCASE
  • TOKEN_NUMERIC
  • TOKEN_SYMBOL
  • TOKEN_SYMBOL_WIDE

Functions

  • array_flatten
  • array_insert
  • array_merge_recursive
  • camelize
  • capitalize
  • downcase
  • dump
  • escape
  • escape_all
  • exact_array_merge_recursive
  • excerpt
  • format
  • generate_token
  • generate_token_wide
  • generate_v4_uuid
  • get_autoconfig
  • humanize
  • hyphenate
  • log
  • log_error
  • log_info
  • log_success
  • log_time
  • normalize
  • normalize_namespace_part
  • normalize_url_path
  • pbkdf2
  • pluralize
  • remove_accents
  • shorten
  • singularize
  • sort_by_weight
  • stable_sort
  • strip_root
  • titleize
  • unaccent_compare
  • unaccent_compare_ci
  • underscore
  • upcase
  1 <?php
  2 
  3 /*
  4  * This file is part of the ICanBoogie package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace ICanBoogie;
 13 
 14 /**
 15  * This class provides an accessor to synthesized low level configurations.
 16  */
 17 class Configs implements \ArrayAccess
 18 {
 19     protected $paths = [];
 20     protected $constructors = [];
 21     protected $configs = [];
 22 
 23     public $cache_repository;
 24 
 25     public function __construct($paths, $constructors)
 26     {
 27         foreach ($paths as $path)
 28         {
 29             $weight = 0;
 30 
 31             if (is_array($path))
 32             {
 33                 $weight = isset($path['weight']) ? $path['weight'] : 0;
 34                 $path = $path[0];
 35             }
 36 
 37             $this->add($path, $weight);
 38         }
 39 
 40         $this->constructors = $constructors;
 41     }
 42 
 43     public function offsetSet($offset, $value)
 44     {
 45         throw new OffsetNotWritable([ $offset, $this ]);
 46     }
 47 
 48     /**
 49      * Checks if a config has been synthesized.
 50      *
 51      * @see ArrayAccess::offsetExists()
 52      */
 53     public function offsetExists($offset)
 54     {
 55         isset($this->configs[$offsets]);
 56     }
 57 
 58     /**
 59      * @throws OffsetNotWritable in attempt to unset an offset.
 60      */
 61     public function offsetUnset($offset)
 62     {
 63         throw new OffsetNotWritable([ $offset, $this ]);
 64     }
 65 
 66     /**
 67      * Returns the specified synthesized configuration.
 68      *
 69      * @see ArrayAccess::offsetGet()
 70      */
 71     public function offsetGet($id)
 72     {
 73         if (isset($this->configs[$id]))
 74         {
 75             return $this->configs[$id];
 76         }
 77 
 78         if (empty($this->constructors[$id]))
 79         {
 80             throw new Exception('There is no constructor defined to build the %id config.', [ '%id' => $id ]);
 81         }
 82 
 83         list($constructor, $from) = $this->constructors[$id] + [ 1 => $id ];
 84 
 85         return $this->synthesize($id, $constructor, $from);
 86     }
 87 
 88     /**
 89      * Revokes the synthsized configs.
 90      *
 91      * The method is usually called after the config paths have been modified.
 92      */
 93     protected function revoke_configs()
 94     {
 95         $this->configs = [];
 96     }
 97 
 98     /**
 99      * Adds a path or several paths to the config.
100      *
101      * Paths are sorted according to their weight. The order in which they were defined is
102      * preserved for paths with the same weight.
103      *
104      * @param string|array $path
105      * @param int $weight Weight of the path. The argument is discarted if `$path` is an array.
106      *
107      * @throws \InvalidArgumentException if the path is empty.
108      */
109     public function add($path, $weight=0)
110     {
111         if (!$path)
112         {
113             throw new \InvalidArgumentException('$path is empty.');
114         }
115 
116         $this->revoke_configs();
117 
118         if (is_array($path))
119         {
120             $combined = array_combine($path, array_fill(0, count($path), $weight));
121 
122             foreach ($combined as $path => $weight)
123             {
124                 if (!file_exists($path))
125                 {
126                     trigger_error(format('Config path %path does not exists', [ 'path' => $path ]));
127                 }
128             }
129 
130             $this->paths += $combined;
131         }
132         else
133         {
134             $this->paths[$path] = $weight;
135         }
136 
137         stable_sort($this->paths);
138     }
139 
140     static private $require_cache = [];
141 
142     static private function isolated_require($__file__, $path)
143     {
144         if (isset(self::$require_cache[$__file__]))
145         {
146             return self::$require_cache[$__file__];
147         }
148 
149         return self::$require_cache[$__file__] = require $__file__;
150     }
151 
152     /**
153      * Returns the fragments of a configuration.
154      *
155      * @param string $name Name of the configuration.
156      *
157      * @return array Where _key_ is the pathname to the fragment file and _value_ the value
158      * returned when the file was required.
159      */
160     public function get_fragments($name)
161     {
162         $fragments = [];
163         $filename = $name . '.php';
164 
165         foreach ($this->paths as $path => $weight)
166         {
167             $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
168             $pathname = $path . $filename;
169 
170             if (!file_exists($pathname))
171             {
172                 continue;
173             }
174 
175             $fragments[$path . $filename] = self::isolated_require($pathname, $path);
176         }
177 
178         return $fragments;
179     }
180 
181     static private $syntheses_cache;
182 
183     /**
184      * Synthesize a configuration.
185      *
186      * @param string $name Name of the configuration to synthesize.
187      * @param string|array $constructor Callback for the synthesis.
188      * @param null|string $from[optional] If the configuration is a derivative $from is the name
189      * of the source configuration.
190      *
191      * @return mixed
192      */
193     public function synthesize($name, $constructor, $from=null)
194     {
195         if (isset($this->configs[$name]))
196         {
197             return $this->configs[$name];
198         }
199 
200         if (!$from)
201         {
202             $from = $name;
203         }
204 
205         $args = [ $from, $constructor ];
206 
207         if ($this->cache_repository)
208         {
209             $cache = self::$syntheses_cache
210             ? self::$syntheses_cache
211             : self::$syntheses_cache = new FileCache
212             ([
213                 FileCache::T_REPOSITORY => $this->cache_repository,
214                 FileCache::T_SERIALIZE => true
215             ]);
216 
217             $rc = $cache->load('config_' . normalize($name, '_'), [ $this, 'synthesize_constructor' ], $args);
218         }
219         else
220         {
221             $rc = $this->synthesize_constructor($args);
222         }
223 
224         $this->configs[$name] = $rc;
225 
226         return $rc;
227     }
228 
229     public function synthesize_constructor(array $userdata)
230     {
231         list($name, $constructor) = $userdata;
232 
233         $fragments = $this->get_fragments($name);
234 
235         if (!$fragments)
236         {
237             return;
238         }
239 
240         if ($constructor == 'merge')
241         {
242             $rc = call_user_func_array('array_merge', $fragments);
243         }
244         else if ($constructor == 'recursive merge')
245         {
246             $rc = call_user_func_array('ICanBoogie\array_merge_recursive', $fragments);
247         }
248         else
249         {
250             $rc = call_user_func($constructor, $fragments);
251         }
252 
253         return $rc;
254     }
255 }
Autodoc API documentation generated by ApiGen 2.8.0