Skip to content

Commit

Permalink
Merge pull request #6 from jmolivas/add-validate-services-file
Browse files Browse the repository at this point in the history
Add validate services file method.
  • Loading branch information
jmolivas authored Dec 27, 2016
2 parents bfab556 + 8d98441 commit 5241f1d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
/phpunit.xml

# Project
extend.yml
extend.config.yml
extend.services.yml
10 changes: 6 additions & 4 deletions console.config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
application:
autowire:
commands:
forced:
'extend:example':
class: '\Drupal\Console\Extend\Command\ExampleCommand'
name: {}
forced: {}
# 'extend:example':
# class: '\Drupal\Console\Extend\Command\ExampleCommand'
name: {}
# 'extend:example':
# class: '\Drupal\Console\Extend\Command\ExampleCommand'
5 changes: 5 additions & 0 deletions console.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
# console.extend_example:
# class: \Drupal\Console\Extend\Command\ExampleCommand
# tags:
# - { name: drupal.command }
108 changes: 81 additions & 27 deletions src/Plugin/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public static function dump(Event $event)
return;
}

$config = __DIR__.'/../../config.yml';
$directory = dirname($config);
$configurationData = file_exists($config)?$yaml->load($config):[];
$directory = realpath(__DIR__.'/../../');

$configFile = __DIR__.'/../../console.config.yml';
$servicesFile = __DIR__.'/../../console.services.yml';

$configData = static::validateConfigFile($configFile, $yaml);
$servicesData = static::validateServicesFile($servicesFile, $yaml);

foreach ($packages as $package) {
$packageDirectory = $directory.'/vendor/'.$package;
Expand All @@ -39,41 +43,58 @@ public static function dump(Event $event)
}

$composerFile = $packageDirectory.'/composer.json';
if (!is_file($composerFile)) {
continue;
}

if (!static::isValidPackageType($composerFile)) {
continue;
}

$configFile = $packageDirectory.'/config.yml';
if (!is_file($configFile)) {
continue;
$configFile = $packageDirectory.'/console.config.yml';
if ($packageConfigData = static::validateConfigFile($configFile, $yaml)) {
$configData = array_merge_recursive(
$configData,
$packageConfigData
);
}

$libraryData = $yaml->load($configFile);
if (!static::isValidAutoWireData($libraryData)) {
continue;
$servicesFile = $packageDirectory.'/console.services.yml';
if ($packageServicesData = static::validateServicesFile($servicesFile, $yaml)) {
$servicesData = array_merge_recursive(
$servicesData,
$packageServicesData
);
}
}

$configurationData = array_merge_recursive(
$configurationData,
$libraryData
if ($configData) {
file_put_contents(
$directory . '/extend.config.yml',
$yaml->dump($configData, false, 0, true)
);
}

if ($configurationData) {
if ($servicesData) {
file_put_contents(
$directory . '/extend.yml',
$yaml->dump($configurationData, false, 0, true)
$directory . '/extend.services.yml',
$yaml->dump($servicesData, false, 0, true)
);
}
}

/**
* @param string $composerFile
*
* @return bool
*/
public static function isValidPackageType($composerFile)
{
if (!is_file($composerFile)) {
return false;
}

$composerContent = json_decode(file_get_contents($composerFile), true);
if (!$composerContent) {
return false;
}

if (!array_key_exists('type', $composerContent)) {
return false;
}
Expand All @@ -82,20 +103,53 @@ public static function isValidPackageType($composerFile)
return $packageType === 'drupal-console-library';
}

public static function isValidAutoWireData($libraryData)
/**
* @param string $configFile
* @param Yaml $yaml
*
* @return array
*/
public static function validateConfigFile($configFile, Yaml $yaml)
{
if (!array_key_exists('application', $libraryData)) {
return false;
if (!is_file($configFile)) {
return [];
}

if (!array_key_exists('autowire', $libraryData['application'])) {
return false;
$packageConfigurationData = $yaml->load($configFile);

if (!array_key_exists('application', $packageConfigurationData)) {
return [];
}

if (!array_key_exists('commands', $libraryData['application']['autowire'])) {
return false;
if (!array_key_exists('autowire', $packageConfigurationData['application'])) {
return [];
}

if (!array_key_exists('commands', $packageConfigurationData['application']['autowire'])) {
return [];
}

return $packageConfigurationData;
}

/**
* @param string $servicesFile
* @param Yaml $yaml
*
* @return array
*/
public static function validateServicesFile($servicesFile, Yaml $yaml)
{
if (!is_file($servicesFile)) {
return [];
}

$packageServicesData = $yaml->load($servicesFile);

if (!array_key_exists('services', $packageServicesData)) {
return [];
}

return true;
return $packageServicesData;
}
}

0 comments on commit 5241f1d

Please sign in to comment.