Skip to content

Commit

Permalink
Turned PdbBlockDervier's component discovery code into a separate ser…
Browse files Browse the repository at this point in the history
…vice.
  • Loading branch information
Phéna Proxima committed Apr 28, 2016
1 parent b265b06 commit 04b6a6f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 59 deletions.
7 changes: 7 additions & 0 deletions pdb.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
pdb.component_discovery:
class: '\Drupal\pdb\ComponentDiscovery'
arguments:
- '@app.root'
- '@module_handler'
- '@info_parser'
75 changes: 75 additions & 0 deletions src/ComponentDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @file
* Contains \Drupal\pdb\ComponentDiscovery.
*/

namespace Drupal\pdb;

use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

class ComponentDiscovery extends ExtensionDiscovery implements ComponentDiscoveryInterface {

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* The info parser.
*
* @var \Drupal\Core\Extension\InfoParserInterface
*/
protected $infoParser;

/**
* ComponentDiscovery constructor.
*
* @param string $root
* The root directory of the Drupal installation.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Extension\InfoParserInterface $info_parser
* The info parser.
*/
public function __construct($root, ModuleHandlerInterface $module_handler, InfoParserInterface $info_parser) {
parent::__construct($root);
$this->moduleHandler = $module_handler;
$this->infoParser = $info_parser;
}

/**
* {@inheritdoc}
*/
public function getComponents() {
// Find components.
$components = $this->scan('pdb');

// Set defaults for module info.
$defaults = array(
'dependencies' => array(),
'description' => '',
'package' => 'Other',
'version' => NULL,
);

// Read info files for each module.
foreach ($components as $key => $component) {
// Look for the info file.
$component->info = $this->infoParser->parse($component->getPathname());
$component->info['path'] = $component->origin . '/' . $component->subpath;

// Merge in defaults and save.
$components[$key]->info = $component->info + $defaults;
}
$this->moduleHandler->alter('component_info', $components);

return $components;
}

}
14 changes: 14 additions & 0 deletions src/ComponentDiscoveryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @file
* Contains \Drupal\pdb\ComponentDiscoveryInterface.
*/

namespace Drupal\pdb;

interface ComponentDiscoveryInterface {

public function getComponents();

}
69 changes: 10 additions & 59 deletions src/Plugin/Derivative/PdbBlockDeriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
namespace Drupal\pdb\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\pdb\ComponentDiscoveryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -21,39 +21,28 @@
class PdbBlockDeriver extends DeriverBase implements ContainerDeriverInterface {

/**
* The module handler.
* The component discovery service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
* @var \Drupal\pdb\ComponentDiscoveryInterface
*/
protected $moduleHandler;

/**
* The info parser.
*
* @var \Drupal\Core\Extension\InfoParserInterface
*/
protected $infoParser;
protected $componentDiscovery;

/**
* PdbBlockDeriver constructor.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Extension\InfoParserInterface $info_parser
* The info parser.
* @param \Drupal\pdb\ComponentDiscoveryInterface $component_discovery
* The component discovery service.
*/
public function __construct(ModuleHandlerInterface $module_handler, InfoParserInterface $info_parser) {
$this->moduleHandler = $module_handler;
$this->infoParser = $info_parser;
public function __construct(ComponentDiscoveryInterface $component_discovery, ModuleHandlerInterface $module_handler, InfoParserInterface $info_parser) {
$this->componentDiscovery = $component_discovery;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('module_handler'),
$container->get('info_parser')
$container->get('pdb.component_discovery')
);
}

Expand All @@ -62,7 +51,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Get all custom blocks which should be rediscovered.
$components = $this->getComponents();
$components = $this->componentDiscovery->getComponents();
foreach ($components as $block_id => $block_info) {
$this->derivatives[$block_id] = $base_plugin_definition;
$this->derivatives[$block_id]['info'] = $block_info->info;
Expand All @@ -75,44 +64,6 @@ public function getDerivativeDefinitions($base_plugin_definition) {
return $this->derivatives;
}

/**
* Helper function to scan and collect component .info.yml data.
*
* This is based on system.module function _system_rebuild_module_data().
*
* @TODO: The results of this need to be cached.
*
* @return \Drupal\Core\Extension\Extension[]
* An associative array of component information.
*/
public function getComponents() {
$listing = new ExtensionDiscovery(\Drupal::root());

// Find components.
$components = $listing->scan('pdb');

// Set defaults for module info.
$defaults = array(
'dependencies' => array(),
'description' => '',
'package' => 'Other',
'version' => NULL,
);

// Read info files for each module.
foreach ($components as $key => $component) {
// Look for the info file.
$component->info = $this->infoParser->parse($component->getPathname());
$component->info['path'] = $component->origin . '/' . $component->subpath;

// Merge in defaults and save.
$components[$key]->info = $component->info + $defaults;
}
$this->moduleHandler->alter('component_info', $components);

return $components;
}

/**
* @param array $contexts
* Contexts as defined in component label.
Expand Down

0 comments on commit 04b6a6f

Please sign in to comment.