Skip to content
Snippets Groups Projects
Commit 7cd43f15 authored by Stephen Lucero's avatar Stephen Lucero
Browse files

Incorporate drush commands from v1.

TO DO:
- Test and confirm compatibility
parent 5180d752
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @file
* Drush commands for the yaml_content module.
......@@ -6,7 +7,7 @@
* @todo Re-implement as universal command compatible with Drupal console.
*/
use \Drupal\yaml_content\ContentLoader\ContentLoader;
use Drupal\yaml_content\ContentLoader\ContentLoaderInterface;
/**
* Implements hook_drush_command().
......@@ -17,21 +18,37 @@ function yaml_content_drush_command() {
$items['yaml-content-import'] = [
'description' => dt('Import yaml content.'),
'aliases' => ['yci'],
'arguments' => [],
'options' => [],
'arguments' => [
'directory' => dt('The directory path where content files may be found.'),
'file' => dt('(Optional) The name of a content file to be imported.'),
],
'options' => [
'create-new' => dt('Set this to create content even if it is already in the system.'),
],
];
$items['yaml-content-import-module'] = [
'description' => dt('Import yaml content from a module.'),
'aliases' => ['ycim'],
'arguments' => [],
'options' => [],
'arguments' => [
'module' => dt('The machine name of a module to be searched for content.'),
'file' => dt('(Optional) The name of a content file to be imported.'),
],
'options' => [
'create-new' => dt('Set this to create content even if it is already in the system.'),
],
];
$items['yaml-content-import-dev'] = [
'description' => dt('Dev debugging import.'),
'aliases' => ['ycid'],
'arguments' => [],
'options' => [],
$items['yaml-content-import-profile'] = [
'description' => dt('Import yaml content from a profile.'),
'aliases' => ['ycip'],
'arguments' => [
'profile' => dt('The machine name of a profile to be searched for content.'),
'file' => dt('(Optional) The name of a content file to be imported.'),
],
'options' => [
'create-new' => dt('Set this to create content even if it is already in the system.'),
],
];
return $items;
......@@ -39,30 +56,23 @@ function yaml_content_drush_command() {
/**
* Import specified yaml content file(s).
*
*
* @param string $directory
* The directory path containing the yaml content file(s) to be imported.
* @param string|string[] $file
* @param string $file
* (Optional) The name of a file to be imported or an array of files to
* import. If this argument is not provided then all files in the directory
* matching `*.content.yml` are queued for import.
*
* @todo Implement file globbing for optional `$file` parameter.
*/
function drush_yaml_content_import($directory, $file = NULL) {
$loader = \Drupal::service('yaml_content.content_loader');
$loader->setContentPath($directory);
// @todo Implement file globbing if `$file` is NULL.
$import_files = is_array($file) ? $file : [$file];
// Identify files for import.
$mask = '/' . (isset($file) ? $file : '.*') . '\.content\.yml/';
$files = drush_yaml_content_discover_files($directory . '/content', $mask);
// @todo Verify files before loading for import.
foreach ($import_files as $file) {
drush_print_r('Importing content: ' . $file);
$loader->parseContent($file);
$loaded = $loader->loadContent();
drush_print_r('Imported ' . count($loaded) . ' items.');
}
_drush_yaml_content_import_files($loader, $files);
}
/**
......@@ -80,43 +90,94 @@ function drush_yaml_content_import($directory, $file = NULL) {
* matching `*.content.yml` are queued for import.
*/
function drush_yaml_content_import_module($module, $file = NULL) {
$path = drupal_get_path('module', $module);
$loader = \Drupal::service('yaml_content.content_loader');
$loader->setContentPath($path);
$path = drupal_get_path('module', $module);
// Identify files for import.
$mask = '/' . (isset($file) ? $file : '.*') . '\.content\.yml/';
$files = drush_yaml_content_discover_files($path . '/content', $mask);
if (!$path) {
// @todo Handle when the module cannot be found.
}
else {
$path .= '/content';
}
_drush_yaml_content_import_files($loader, $files);
}
/**
* Import specified yaml content file(s) from a designated profile.
*
* @param string $profile
* The profile to look for content files within.
*
* This command assumes files will be contained within a `content/` directory
* at the top of the module's main directory. Any files within matching the
* pattern `*.content.yml` will then be imported.
* @param string|string[] $file
* (Optional) The name of a file to be imported or an array of files to
* import. If this argument is not provided then all files in the directory
* matching `*.content.yml` are queued for import.
*/
function drush_yaml_content_import_profile($profile, $file = NULL) {
$path = drupal_get_path('profile', $profile);
$loader = \Drupal::service('yaml_content.content_loader');
$loader->setContentPath($path);
// Identify files for import.
if (is_null($file)) {
$mask = '/.*\.content\.yml/';
}
else {
// Scan only for the specific file name if it exists.
$mask = '/' . $file . '\.content\.yml/';
}
$files = file_scan_directory($path, $mask, ['recurse' => FALSE]);
$mask = '/' . (isset($file) ? $file : '.*') . '\.content\.yml/';
$files = drush_yaml_content_discover_files($path . '/content', $mask);
_drush_yaml_content_import_files($loader, $files);
}
// @todo Verify files before loading for import.
foreach ($files as $filename => $file) {
drush_print_r('Importing content: ' . $filename);
$loader->parseContent($file->filename);
$loaded = $loader->loadContent();
drush_print_r('Imported ' . count($loaded) . ' items.');
}
/**
* Scan and discover content files for import.
*
* The scanner assumes all content files will follow the naming convention of
* '*.content.yml'.
*
* @param string $path
* The directory path to be scanned for content files.
* @param string $mask
* (Optional) A file name mask to limit matches in scanned files.
*
* @return array
* An associative array of objects keyed by filename with the following
* properties as returned by file_scan_directory():
*
* - 'uri'
* - 'filename'
* - 'name'
*
* @see file_scan_directory()
*/
function drush_yaml_content_discover_files($path, $mask = '/.*\.content\.yml/') {
// Identify files for import.
$files = file_scan_directory($path, $mask, [
'key' => 'filename',
'recurse' => FALSE,
]);
return $files;
}
/**
* Import debugging content.
* Import content files using a Content Loader.
*
* @param \Drupal\yaml_content\ContentLoader\ContentLoaderInterface $loader
* The content loader to use to import the referenced files.
* @param array $files
* An array of file descriptors as loaded by file_scan_directory() keyed by
* filename. Each of the listed files will be imported.
*/
function drush_yaml_content_import_dev() {
$content_path = drupal_get_path('module', 'yaml_content') . '/content';
drush_yaml_content_import($content_path, 'dev.content.yml');
function _drush_yaml_content_import_files(ContentLoaderInterface $loader, array $files) {
// @todo Verify files before loading for import.
foreach ($files as $filename => $file) {
drush_print_r(dt('Importing content: @file', [
'@file' => $filename,
]));
$loaded = $loader->loadContent($filename);
drush_print_r(dt('Imported @count items', [
'@count' => count($loaded),
]));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment