Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / src / BlazyManagerBase.php
1 <?php
2
3 namespace Drupal\blazy;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Render\RendererInterface;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Cache\Cache;
10 use Drupal\Core\Cache\CacheBackendInterface;
11 use Drupal\Component\Utility\NestedArray;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Implements BlazyManagerInterface.
16  */
17 abstract class BlazyManagerBase implements BlazyManagerInterface {
18
19   /**
20    * The entity type manager service.
21    *
22    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23    */
24   protected $entityTypeManager;
25
26   /**
27    * The module handler service.
28    *
29    * @var \Drupal\Core\Extension\ModuleHandlerInterface
30    */
31   protected $moduleHandler;
32
33   /**
34    * The renderer.
35    *
36    * @var \Drupal\Core\Render\RendererInterface
37    */
38   protected $renderer;
39
40   /**
41    * The config factory.
42    *
43    * @var \Drupal\Core\Config\ConfigFactoryInterface
44    */
45   protected $configFactory;
46
47   /**
48    * The cache backend.
49    *
50    * @var \Drupal\Core\Cache\CacheBackendInterface
51    */
52   protected $cache;
53
54   /**
55    * The supported lightboxes.
56    *
57    * @var array
58    */
59   protected $lightboxes = [];
60
61   /**
62    * Constructs a BlazyManager object.
63    */
64   public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache) {
65     $this->entityTypeManager = $entity_type_manager;
66     $this->moduleHandler     = $module_handler;
67     $this->renderer          = $renderer;
68     $this->configFactory     = $config_factory;
69     $this->cache             = $cache;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public static function create(ContainerInterface $container) {
76     return new static(
77       $container->get('entity_type.manager'),
78       $container->get('module_handler'),
79       $container->get('renderer'),
80       $container->get('config.factory'),
81       $container->get('cache.default')
82     );
83   }
84
85   /**
86    * Returns the entity type manager.
87    */
88   public function getEntityTypeManager() {
89     return $this->entityTypeManager;
90   }
91
92   /**
93    * Returns the module handler.
94    */
95   public function getModuleHandler() {
96     return $this->moduleHandler;
97   }
98
99   /**
100    * Returns the renderer.
101    */
102   public function getRenderer() {
103     return $this->renderer;
104   }
105
106   /**
107    * Returns the config factory.
108    */
109   public function getConfigFactory() {
110     return $this->configFactory;
111   }
112
113   /**
114    * Returns the cache.
115    */
116   public function getCache() {
117     return $this->cache;
118   }
119
120   /**
121    * Returns any config, or keyed by the $setting_name.
122    */
123   public function configLoad($setting_name = '', $settings = 'blazy.settings') {
124     $config  = $this->configFactory->get($settings);
125     $configs = $config->get();
126     unset($configs['_core']);
127     return empty($setting_name) ? $configs : $config->get($setting_name);
128   }
129
130   /**
131    * Returns a shortcut for loading a config entity: image_style, slick, etc.
132    */
133   public function entityLoad($id, $entity_type = 'image_style') {
134     return $this->entityTypeManager->getStorage($entity_type)->load($id);
135   }
136
137   /**
138    * Returns a shortcut for loading multiple configuration entities.
139    */
140   public function entityLoadMultiple($entity_type = 'image_style', $ids = NULL) {
141     return $this->entityTypeManager->getStorage($entity_type)->loadMultiple($ids);
142   }
143
144   /**
145    * Returns array of needed assets suitable for #attached property.
146    */
147   public function attach($attach = []) {
148     $load   = [];
149     $dummy  = [];
150     $switch = empty($attach['media_switch']) ? '' : $attach['media_switch'];
151
152     if ($switch && $switch != 'content') {
153       $attach[$switch] = $switch;
154
155       if (in_array($switch, $this->getLightboxes())) {
156         $load['library'][] = 'blazy/lightbox';
157       }
158     }
159
160     // Only load grid xor column, but not both.
161     $attach['column'] = !empty($attach['style']) && $attach['style'] == 'column';
162     if (!empty($attach['column'])) {
163       $attach['grid'] = FALSE;
164     }
165     foreach (['column', 'grid', 'media', 'photobox', 'ratio'] as $component) {
166       if (!empty($attach[$component])) {
167         $load['library'][] = 'blazy/' . $component;
168       }
169     }
170
171     // Core Blazy libraries.
172     if (!empty($attach['blazy'])) {
173       $load['library'][] = 'blazy/load';
174       $load['drupalSettings']['blazy'] = $this->configLoad()['blazy'];
175     }
176
177     $this->moduleHandler->alter('blazy_attach', $load, $attach);
178     return $load;
179   }
180
181   /**
182    * Collects defined skins as registered via hook_MODULE_NAME_skins_info().
183    */
184   public function buildSkins($namespace, $skin_class, $methods = []) {
185     $cid = $namespace . ':skins';
186     $cache = $this->cache->get($cid);
187
188     if ($cache) {
189       return $cache->data;
190     }
191
192     $classes = $this->moduleHandler->invokeAll($namespace . '_skins_info');
193     $classes = array_merge([$skin_class], $classes);
194     $items   = $skins = [];
195     foreach ($classes as $class) {
196       if (class_exists($class)) {
197         $reflection = new \ReflectionClass($class);
198         if ($reflection->implementsInterface($skin_class . 'Interface')) {
199           $skin = new $class();
200           if (empty($methods) && method_exists($skin, 'skins')) {
201             $items = $skin->skins();
202           }
203           else {
204             foreach ($methods as $method) {
205               $items[$method] = method_exists($skin, $method) ? $skin->{$method}() : [];
206             }
207           }
208         }
209       }
210       $skins = NestedArray::mergeDeep($skins, $items);
211     }
212
213     $count = isset($items['skins']) ? count($items['skins']) : count($items);
214     $tags  = Cache::buildTags($cid, ['count:' . $count]);
215
216     $this->cache->set($cid, $skins, Cache::PERMANENT, $tags);
217
218     return $skins;
219   }
220
221   /**
222    * Gets the supported lightboxes.
223    *
224    * @return array
225    *   The supported lightboxes.
226    */
227   public function getLightboxes() {
228     $boxes = $this->lightboxes + ['colorbox', 'photobox'];
229
230     $lightboxes = [];
231     foreach (array_unique($boxes) as $lightbox) {
232       if (function_exists($lightbox . '_theme')) {
233         $lightboxes[] = $lightbox;
234       }
235     }
236
237     $this->moduleHandler->alter('blazy_lightboxes', $lightboxes);
238     return array_unique($lightboxes);
239   }
240
241   /**
242    * Sets the lightboxes.
243    *
244    * @param string $lightbox
245    *   The lightbox name, expected to be the module name.
246    */
247   public function setLightboxes($lightbox) {
248     $this->lightboxes[] = $lightbox;
249   }
250
251 }