Commit 03e535ca authored by alexpott's avatar alexpott

Issue #2050289 by damiankloip, msonnabaum: Add class to make yaml discovery reusable.

parent 230432ad
<?php
/**
* @file
* Contains \Drupal\Core\Discovery\DiscoverableInterface.
*/
namespace Drupal\Component\Discovery;
/**
* Interface for classes providing a type of discovery.
*/
interface DiscoverableInterface {
/**
* Returns an array of discoverable items.
*
* @return array
* An array of discovered data.
*/
public function findAll();
}
<?php
/**
* @file
* Contains \Drupal\Component\Discovery\YamlDiscovery.
*/
namespace Drupal\Component\Discovery;
use Symfony\Component\Yaml\Parser;
/**
* Provides discovery for YAML files within a given set of directories.
*/
class YamlDiscovery implements DiscoverableInterface {
/**
* The base filename to look for in each directory.
*
* @var string
*/
protected $name;
/**
* An array of directories to scan.
*
* @var array
*/
protected $directories = array();
/**
* The symfony YAML parser.
*
* @var \Symfony\Component\Yaml\Parser
*/
protected $parser;
/**
* Constructs a YamlDiscovery object.
*
* @param string $name
* The
* @param array $directories
* An array of directories to scan. If an associative array is provided the
* return array will use these keys.
*/
public function __construct($name, array $directories) {
$this->name = $name;
$this->directories = $directories;
}
/**
* {@inheritdoc}
*/
public function findAll() {
$all = array();
$parser = $this->parser();
foreach ($this->findFiles() as $key => $file) {
$all[$key] = $parser->parse(file_get_contents($file));
}
return $all;
}
/**
* Returns the YAML parse.
*
* @return \Symfony\Component\Yaml\Parser
* The symfony YAML parser.
*/
protected function parser() {
if (!isset($this->parser)) {
$this->parser = new Parser();
}
return $this->parser;
}
/**
* Returns an array of file paths.
*
* @return array
*/
protected function findFiles() {
$files = array();
foreach ($this->directories as $key => $directory) {
$file = $directory . '/' . $this->fileBaseName($directory) . '.yml';
if (file_exists($file)) {
$files[$key] = $file;
}
}
return $files;
}
/**
* Returns the base filename for the current directory.
*
* @param string $directory
* The current directory path.
*
* @return string
* The file name, without the .yml extension.
*/
protected function fileBaseName($directory) {
return basename($directory) . '.' . $this->name;
}
}
......@@ -900,4 +900,14 @@ public function uninstall($module_list = array(), $uninstall_dependents = TRUE)
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getModuleDirectories() {
$dirs = array();
foreach ($this->getModuleList() as $module => $filename) {
$dirs[$module] = DRUPAL_ROOT . '/' . dirname($filename);
}
return $dirs;
}
}
......@@ -335,4 +335,12 @@ public function disable($module_list, $disable_dependents = TRUE);
*/
public function uninstall($module_list = array(), $uninstall_dependents = TRUE);
/**
* Returns an array of directories for all enabled modules. Useful for
* tasks such as finding a file that exists in all module directories.
*
* @return array
*/
public function getModuleDirectories();
}
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Routing;
use Drupal\Component\Discovery\YamlDiscovery;
use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Yaml\Parser;
......@@ -81,25 +82,22 @@ public function rebuild() {
return;
}
$parser = new Parser();
$yaml_discovery = new YamlDiscovery('routing', $this->moduleHandler->getModuleDirectories());
// We need to manually call each module so that we can know which module
// a given item came from.
foreach ($this->moduleHandler->getModuleList() as $module => $filename) {
foreach ($yaml_discovery->findAll() as $module => $routes) {
$collection = new RouteCollection();
$routing_file = DRUPAL_ROOT . '/' . dirname($filename) . '/' . $module . '.routing.yml';
if (file_exists($routing_file)) {
$routes = $parser->parse(file_get_contents($routing_file));
if (!empty($routes)) {
foreach ($routes as $name => $route_info) {
$defaults = isset($route_info['defaults']) ? $route_info['defaults'] : array();
$requirements = isset($route_info['requirements']) ? $route_info['requirements'] : array();
$options = isset($route_info['options']) ? $route_info['options'] : array();
$route = new Route($route_info['pattern'], $defaults, $requirements, $options);
$collection->add($name, $route);
}
}
foreach ($routes as $name => $route_info) {
$route_info += array(
'defaults' => array(),
'requirements' => array(),
'options' => array(),
);
$route = new Route($route_info['pattern'], $route_info['defaults'], $route_info['requirements'], $route_info['options']);
$collection->add($name, $route);
}
$this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, $module));
$this->dumper->addRoutes($collection);
$this->dumper->dump(array('route_set' => $module));
......
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Discovery\YamlDiscoveryTest.
*/
namespace Drupal\Tests\Component\Discovery;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Discovery\YamlDiscovery;
/**
* Tests the YamlDiscovery component class.
*/
class YamlDiscoveryTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'YamlDiscovery',
'description' => 'YamlDiscovery component unit tests.',
'group' => 'Discovery',
);
}
/**
* Tests the YAML file discovery.
*/
public function testDiscovery() {
$base_path = __DIR__ . '/Fixtures';
// Set up the directories to search.
$directories = array(
'test_1' => $base_path . '/test_1',
'test_2' => $base_path . '/test_2',
);
$discovery = new YamlDiscovery('test', $directories);
$data = $discovery->findAll();
$this->assertEquals(count($data), 2);
$this->assertArrayHasKey('test_1', $data);
$this->assertArrayHasKey('test_2', $data);
foreach ($data as $item) {
$this->assertArrayHasKey('name', $item);
$this->assertEquals($item['name'], 'test');
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment