You are here

class VisibilityChecker in Image Lazyloader 8

Class VisibilityChecker.

Hierarchy

Expanded class hierarchy of VisibilityChecker

1 string reference to 'VisibilityChecker'
lazyloader.services.yml in ./lazyloader.services.yml
lazyloader.services.yml
1 service uses VisibilityChecker
lazyloader.visibility_checker in ./lazyloader.services.yml
\Drupal\lazyloader\VisibilityChecker

File

src/VisibilityChecker.php, line 14

Namespace

Drupal\lazyloader
View source
class VisibilityChecker {
  use ConditionAccessResolverTrait;

  /**
   * The image style storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $imageStyleStorage;

  /**
   * The condition manager.
   *
   * @var \Drupal\Core\Condition\ConditionManager
   */
  protected $conditionManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Creates a new VisibilityChecker instance.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Condition\ConditionManager $condition_manager
   *   The condition manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, ConditionManager $condition_manager) {
    $this->configFactory = $config_factory;
    $this->imageStyleStorage = $entity_type_manager
      ->getStorage('image_style');
    $this->conditionManager = $condition_manager;
  }

  /**
   * Gets the condition list.
   *
   * @return \Drupal\Core\Condition\ConditionPluginCollection
   *   The condition list.
   */
  protected function getConditionList() {
    return new ConditionPluginCollection($this->conditionManager, $this->configFactory
      ->get('lazyloader.exclude')
      ->get('visibility'));
  }

  /**
   * Asserts if Lazyloader is enabled.
   *
   * @return bool
   *   Whether or not Lazyloader is enabled.
   */
  public function isEnabled() {
    $enabled = $this->configFactory
      ->get('lazyloader.configuration')
      ->get('enabled');
    $conditions_apply = $this
      ->resolveConditions(iterator_to_array($this
      ->getConditionList()
      ->getIterator()), 'and');
    return $enabled && $conditions_apply;
  }

  /**
   * Asserts if the uri is a valid filename.
   *
   * @param string $uri
   *   The uri.
   *
   * @return bool
   *   Whether or not the uri is a valid filename.
   */
  public function isValidFilename($uri) {
    $excluded_files = $this->configFactory
      ->get('lazyloader.exclude')
      ->get('filenames');
    $parts = explode('/', $uri);
    $parts = explode('?', array_pop($parts));
    $filename = array_shift($parts);
    return !(bool) preg_match('/^' . $filename . '$/m', $excluded_files);
  }

  /**
   * Asserts if a uri is a valid image style.
   *
   * @param string $uri
   *   The uri.
   *
   * @return bool
   *   Whether or not this is a valid image style.
   */
  public function isValidImageStyle($uri) {
    $excluded_styles = $this
      ->filterSelectedValues($this->configFactory
      ->get('lazyloader.exclude')
      ->get('image_styles'));

    // If no image styles are selected we have nothing to exclude.
    if (empty($excluded_styles)) {
      return TRUE;
    }
    $styles = implode('|', array_keys($this->imageStyleStorage
      ->loadMultiple()));

    // Make sure the image is actually a derived image.
    if (!preg_match('/styles\\/[' . $styles . ']/', $uri)) {

      // Not a derived image, nothing to do here.
      return TRUE;
    }
    $excluded_styles = implode('|', $excluded_styles);
    return !preg_match('/styles\\/[' . $excluded_styles . ']/', $uri);
  }

  /**
   * Filters an array of selected checkbox values.
   *
   * @todo Ideally we would trust the stored data in config.
   */
  protected function filterSelectedValues($values) {

    // Filter out deselected values.
    foreach ((array) $values as $key => $value) {
      if (!$value) {
        unset($values[$key]);
      }
    }
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionAccessResolverTrait::resolveConditions protected function Resolves the given conditions based on the condition logic ('and'/'or').
VisibilityChecker::$conditionManager protected property The condition manager.
VisibilityChecker::$configFactory protected property The config factory.
VisibilityChecker::$imageStyleStorage protected property The image style storage.
VisibilityChecker::filterSelectedValues protected function Filters an array of selected checkbox values.
VisibilityChecker::getConditionList protected function Gets the condition list.
VisibilityChecker::isEnabled public function Asserts if Lazyloader is enabled.
VisibilityChecker::isValidFilename public function Asserts if the uri is a valid filename.
VisibilityChecker::isValidImageStyle public function Asserts if a uri is a valid image style.
VisibilityChecker::__construct public function Creates a new VisibilityChecker instance.