crossword-8.x-1.x-dev/src/CrosswordFileParserPluginBase.php
src/CrosswordFileParserPluginBase.php
<?php
namespace Drupal\crossword;
use Drupal\Core\Plugin\PluginBase;
use Drupal\file\Entity\File;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for Crossword File Parser Plugins.
*/
abstract class CrosswordFileParserPluginBase extends PluginBase implements CrosswordFileParserPluginInterface, ContainerFactoryPluginInterface {
/**
* The file entity that hopefully represents a crossword.
*
* @var \Drupal\file\FileInterface
*/
protected $file;
/**
* The contents of the file.
*
* @var string
*/
protected $contents;
/**
* Create a plugin with the given input.
*
* @param string $configuration
* The configuration of the plugin.
* @param string $plugin_id
* The plugin id.
* @param array $plugin_definition
* The plugin definition.
*
* @throws \Exception
*/
public function __construct($configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->file = File::load($configuration['fid']);
if (!static::isApplicable($this->file)) {
throw new \Exception('Chosen crossword file parser cannot parse this file.');
}
$this->contents = file_get_contents($this->file->getFileUri());
$this->contents = trim($this->contents);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition
);
}
}
