You are here

public function BlazyManagerBase::buildSkins in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/BlazyManagerBase.php \Drupal\blazy\BlazyManagerBase::buildSkins()
  2. 7 src/BlazyManagerBase.php \Drupal\blazy\BlazyManagerBase::buildSkins()

Collects defined skins as registered via hook_MODULE_NAME_skins_info().

File

src/BlazyManagerBase.php, line 178

Class

BlazyManagerBase
Implements BlazyManagerInterface.

Namespace

Drupal\blazy

Code

public function buildSkins($namespace, $skin_class, $methods = []) {
  $cid = $namespace . ':skins';
  if ($cache = $this->cache
    ->get($cid)) {
    return $cache->data;
  }
  $classes = $this->moduleHandler
    ->invokeAll($namespace . '_skins_info');
  $classes = array_merge([
    $skin_class,
  ], $classes);
  $items = $skins = [];
  foreach ($classes as $class) {
    if (class_exists($class)) {
      $reflection = new \ReflectionClass($class);
      if ($reflection
        ->implementsInterface($skin_class . 'Interface')) {
        $skin = new $class();
        if (empty($methods) && method_exists($skin, 'skins')) {
          $items = $skin
            ->skins();
        }
        else {
          foreach ($methods as $method) {
            $items[$method] = method_exists($skin, $method) ? $skin
              ->{$method}() : [];
          }
        }
      }
    }
    $skins = NestedArray::mergeDeep($skins, $items);
  }
  $count = isset($items['skins']) ? count($items['skins']) : count($items);
  $tags = Cache::buildTags($cid, [
    'count:' . $count,
  ]);
  $this->cache
    ->set($cid, $skins, Cache::PERMANENT, $tags);
  return $skins;
}