Autodoc
  • Namespace
  • Constant
  • 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

  • AdjustThumbnailOptions
  • AdjustThumbnailVersion
  • CacheManager
  • GetOperation
  • Hooks
  • Module
  • PopThumbnailVersion
  • Thumbnail
  • Version
  • Versions

Exceptions

  • VersionNotDefined

Constants

  • CACHE_VERSIONS
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee 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\Modules\Thumbnailer;
 13 
 14 const CACHE_VERSIONS = true;
 15 
 16 class Versions implements \ArrayAccess, \IteratorAggregate
 17 {
 18     /**
 19      * Creates a {@link Versions} instance and initializes it with the defined versions.
 20      *
 21      * The event `ICanBoogie\Modules\Thumbnailer\Versions::alter` of class
 22      * {@link ICanBoogie\Modules\Thumbnailer\Versions\AlterEvent} is fired to allow third parties
 23      * to alter the instance.
 24      *
 25      * @param \ICanBoogie\Core $core
 26      */
 27     static public function prototype_get_thumbnailer_versions(\ICanBoogie\Core $core)
 28     {
 29         if (CACHE_VERSIONS)
 30         {
 31             $versions = $core->vars['cached_thumbnailer_versions'];
 32 
 33             if (!$versions)
 34             {
 35                 $versions = self::collect($core);
 36 
 37                 $core->vars['cached_thumbnailer_versions'] = $versions;
 38             }
 39         }
 40         else
 41         {
 42             $versions = self::collect($core);
 43         }
 44 
 45         $instance = new static($versions);
 46 
 47         new Versions\AlterEvent($instance);
 48 
 49         return $instance;
 50     }
 51 
 52     /**
 53      * Collects versions.
 54      *
 55      * @return array[string]array
 56      */
 57     static private function collect(\ICanBoogie\Core $core)
 58     {
 59         $versions = array();
 60         $definitions = $core->registry
 61         ->select('SUBSTR(name, LENGTH("thumbnailer.versions.") + 1) as name, value')
 62         ->where('name LIKE ?', 'thumbnailer.versions.%')
 63         ->pairs;
 64 
 65         foreach ($definitions as $name => $options)
 66         {
 67             if (!$options || !is_string($options) || $options{0} != '{')
 68             {
 69                 \ICanBoogie\log_error('Bad version: %name, :options', array('name' => $name, 'options' => $options));
 70 
 71                 continue;
 72             }
 73 
 74             $versions[$name] = Version::normalize(json_decode($options, true));
 75         }
 76 
 77         return $versions;
 78     }
 79 
 80     /**
 81      * Defined versions.
 82      *
 83      * @var array[string]mixed
 84      */
 85     protected $versions;
 86 
 87     /**
 88      * Initializes the specified versions.
 89      *
 90      * @param array $versions
 91      */
 92     public function __construct(array $versions=array())
 93     {
 94         foreach ($versions as $name => $version)
 95         {
 96             $this[$name] = $version;
 97         }
 98     }
 99 
100     /**
101      * Saves the versions.
102      */
103     public function save()
104     {
105         foreach ($this->versions as $name => $version)
106         {
107             $this->save_version($name, $version);
108         }
109     }
110 
111     /**
112      * Persists a version.
113      *
114      * @param string $name Name of the version.
115      * @param Version|array|string $version Version to persist. The version can be specified as
116      * a {@link Version} instance, an array or as a string (a serialized version).
117      *
118      * @return array The options actually saved.
119      */
120     public function save_version($name, $version)
121     {
122         global $core;
123 
124         if (!($version instanceof Versions))
125         {
126             $version = new Version($version);
127         }
128 
129         $options = $version->to_array(Version::ARRAY_FILTER | Version::ARRAY_SHORTEN);
130         $core->registry["thumbnailer.versions.$name"] = json_encode($options);
131 
132         # revoke cache
133 
134         unset($core->vars['cached_thumbnailer_versions']);
135 
136         return $options;
137     }
138 
139     /**
140      * Checks if a version exists.
141      */
142     public function offsetExists($version)
143     {
144         return isset($this->versions[$version]);
145     }
146 
147     /**
148      * Returns the definition of a version.
149      *
150      * @throws VersionNotDefined in attempt to get a version that is not defined.
151      */
152     public function offsetGet($version)
153     {
154         if (!$this->offsetExists($version))
155         {
156             throw new VersionNotDefined($version);
157         }
158 
159         $v = $this->versions[$version];
160 
161         if (!($v instanceof Version))
162         {
163             $v = new Version($v);
164             $this->versions[$version] = $v;
165         }
166 
167         return $v;
168     }
169 
170     /**
171      * Sets a version.
172      *
173      * @param string $version Name of the version
174      * @param array[string]mixed Options of the version.
175      */
176     public function offsetSet($version, $options)
177     {
178         $this->versions[$version] = $options;
179     }
180 
181     /**
182      * Deletes a version.
183      */
184     public function offsetUnset($version)
185     {
186         unset($this->versions[$version]);
187     }
188 
189     public function getIterator()
190     {
191         return new \ArrayIterator($this->versions);
192     }
193 }
194 
195 /*
196  * Exception
197  */
198 
199 /**
200  * Exception thrown when a thumbnail version is not defined.
201  *
202  * @property-read string $version The thumbnail version identifier.
203  */
204 class VersionNotDefined extends \InvalidArgumentException
205 {
206     private $version;
207 
208     public function __construct($version, $code=500, \Exception $previous=null)
209     {
210         $this->version = $version;
211 
212         parent::__construct("Version not defined: $version.", $code, $previous);
213     }
214 
215     public function __get($property)
216     {
217         if ($property == 'version')
218         {
219             return $this->version;
220         }
221     }
222 }
223 
224 namespace ICanBoogie\Modules\Thumbnailer\Versions;
225 
226 /**
227  * Event class for the `ICanBoogie\Modules\Thumbnailer\Versions::alert` event.
228  */
229 class AlterEvent extends \ICanBoogie\Event
230 {
231     /**
232      * The event is constructed with the type `alter`.
233      *
234      * @param \ICanBoogie\Modules\Thumbnailer\Versions $target
235      */
236     public function __construct(\ICanBoogie\Modules\Thumbnailer\Versions $target)
237     {
238         parent::__construct($target, 'alter');
239     }
240 }
Autodoc API documentation generated by ApiGen 2.8.0