diff --git a/core/lib/Drupal/Component/Scaffold/ComposerScaffoldCommand.php b/core/lib/Drupal/Component/Scaffold/ComposerScaffoldCommand.php index 5b575b9651..85e253ea3f 100644 --- a/core/lib/Drupal/Component/Scaffold/ComposerScaffoldCommand.php +++ b/core/lib/Drupal/Component/Scaffold/ComposerScaffoldCommand.php @@ -35,7 +35,7 @@ protected function configure() { allowed to scaffold in the top-level composer.json will be processed by this command. -For more information, see https://drupal.org/node/@TODO +For more information, see https://www.drupal.org/docs/develop/using-composer/using-drupals-composer-scaffold. EOT ); diff --git a/core/lib/Drupal/Component/Scaffold/Handler.php b/core/lib/Drupal/Component/Scaffold/Handler.php index a6c14dca22..697d0a1403 100644 --- a/core/lib/Drupal/Component/Scaffold/Handler.php +++ b/core/lib/Drupal/Component/Scaffold/Handler.php @@ -165,10 +165,9 @@ public function scaffold() { // Analyze the list of file mappings, and determine which take priority. $scaffoldCollection = new OperationCollection($this->io); $locationReplacements = $this->manageOptions->getLocationReplacements(); - $scaffoldCollection->collateScaffoldFiles($file_mappings, $locationReplacements); // Write the collected scaffold files to the designated location on disk. - $scaffoldResults = $scaffoldCollection->processScaffoldFiles($this->manageOptions->getOptions()); + $scaffoldResults = $scaffoldCollection->process($file_mappings, $locationReplacements, $this->manageOptions->getOptions()); // Generate an autoload file in the document root that includes the // autoload.php file in the vendor directory, wherever that is. Drupal diff --git a/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php b/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php index 2f656d03da..eeb47f0bd3 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php @@ -27,7 +27,7 @@ class AppendOp implements OperationInterface, PreprocessOriginalOpInterface { protected $append; /** - * Sets the relative path to the prepend file. + * Constructs an AppendOp. * * @param \Drupal\Component\Scaffold\ScaffoldFilePath $prependPath * The relative path to the prepend file. diff --git a/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php b/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php index d76757cedc..3d22caede1 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php @@ -13,20 +13,6 @@ */ class OperationCollection { - /** - * Associative array containing destination => operation mappings. - * - * @var array - */ - protected $listOfScaffoldFiles; - - /** - * Associative array containing package name => file mappings. - * - * @var array - */ - protected $resolvedFileMappings; - /** * Composer's I/O service. * @@ -45,26 +31,6 @@ public function __construct(IOInterface $io) { $this->io = $io; } - /** - * Fetches the file mappings. - * - * @return array - * Associative array containing package name => file mappings - */ - public function fileMappings() { - return $this->resolvedFileMappings; - } - - /** - * Gets the final list of what should be scaffolded where. - * - * @return array - * Associative array containing destination => operation mappings - */ - public function scaffoldList() { - return $this->listOfScaffoldFiles; - } - /** * Finds the package name that provides the scaffold file. * @@ -74,24 +40,37 @@ public function scaffoldList() { * this will be the same as $scaffold_file->packageName() unless this scaffold * file has been overridden or removed by some other package. * + * @param array $list_of_scaffold_files + * Associative array containing destination => operation mappings. * @param \Drupal\Component\Scaffold\ScaffoldFileInfo $scaffold_file * The scaffold file to use to find a providing package name. * * @return string * The name of the package that provided the scaffold file information. */ - public function findProvidingPackage(ScaffoldFileInfo $scaffold_file) { + protected function findProvidingPackage(array $list_of_scaffold_files, ScaffoldFileInfo $scaffold_file) { // The scaffold file should always be in our list, but we will check // just to be sure that it really is. - $scaffoldList = $this->scaffoldList(); $dest_rel_path = $scaffold_file->destination()->relativePath(); - if (!array_key_exists($dest_rel_path, $scaffoldList)) { + if (!array_key_exists($dest_rel_path, $list_of_scaffold_files)) { throw new \RuntimeException("Scaffold file not found in list of all scaffold files."); } - $overridden_scaffold_file = $scaffoldList[$dest_rel_path]; + $overridden_scaffold_file = $list_of_scaffold_files[$dest_rel_path]; return $overridden_scaffold_file->packageName(); } + /** + * Description + * @param array $file_mappings + * @param Interpolator $locationReplacements + * @param ScaffoldOptions $options + * @return type + */ + public function process(array $file_mappings, Interpolator $locationReplacements, ScaffoldOptions $options) { + list($list_of_scaffold_files, $resolved_file_mappings) = $this->collateScaffoldFiles($file_mappings, $locationReplacements); + return $this->processScaffoldFiles($list_of_scaffold_files, $resolved_file_mappings, $options); + } + /** * Copy all files, as defined by $file_mappings. * @@ -100,8 +79,13 @@ public function findProvidingPackage(ScaffoldFileInfo $scaffold_file) { * self::getFileMappingsFromPackages(). * @param \Drupal\Component\Scaffold\Interpolator $locationReplacements * An object with the location mappings (e.g. [web-root]). + * + * @return array + * A list containing two lists: + * - Associative array containing destination => operation mappings. + * - Associative array containing package name => file mappings. */ - public function collateScaffoldFiles(array $file_mappings, Interpolator $locationReplacements) { + protected function collateScaffoldFiles(array $file_mappings, Interpolator $locationReplacements) { $resolved_file_mappings = []; $list_of_scaffold_files = []; foreach ($file_mappings as $package_name => $package_file_mappings) { @@ -118,13 +102,16 @@ public function collateScaffoldFiles(array $file_mappings, Interpolator $locatio $resolved_file_mappings[$package_name][$destination_rel_path] = $scaffold_file; } } - $this->listOfScaffoldFiles = $list_of_scaffold_files; - $this->resolvedFileMappings = $resolved_file_mappings; + return [$list_of_scaffold_files, $resolved_file_mappings]; } /** * Scaffolds the files in our scaffold collection, package-by-package. * + * @param array $list_of_scaffold_files + * Associative array containing destination => operation mappings. + * @param array $resolved_file_mappings + * Associative array containing package name => file mappings. * @param \Drupal\Component\Scaffold\ScaffoldOptions $options * Configuration options from the top-level composer.json file. * @@ -132,7 +119,7 @@ public function collateScaffoldFiles(array $file_mappings, Interpolator $locatio * Associative array keyed by destination path and values as the scaffold * result for each scaffolded file. */ - public function processScaffoldFiles(ScaffoldOptions $options) { + protected function processScaffoldFiles(array $list_of_scaffold_files, array $resolved_file_mappings, ScaffoldOptions $options) { $result = []; // We could simply scaffold all of the files from $list_of_scaffold_files, // which contain only the list of files to be processed. We iterate over @@ -140,10 +127,10 @@ public function processScaffoldFiles(ScaffoldOptions $options) { // scaffold files grouped by the package that provided them, including // those not being scaffolded (because they were overridden or removed // by some later package). - foreach ($this->fileMappings() as $package_name => $package_scaffold_files) { + foreach ($resolved_file_mappings as $package_name => $package_scaffold_files) { $this->io->write("Scaffolding files for {$package_name}:"); foreach ($package_scaffold_files as $dest_rel_path => $scaffold_file) { - $overriding_package = $this->findProvidingPackage($scaffold_file); + $overriding_package = $this->findProvidingPackage($list_of_scaffold_files, $scaffold_file); if ($scaffold_file->overridden($overriding_package)) { $this->io->write($scaffold_file->interpolate(" - Skip [dest-rel-path]: overridden in {$overriding_package}")); } diff --git a/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php b/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php index 34846b9811..ad6f531837 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php @@ -105,8 +105,8 @@ protected function createReplaceOp(PackageInterface $package, $dest_rel_path, ar protected function createAppendOp(PackageInterface $package, $dest_rel_path, array $metadata) { $package_name = $package->getName(); $package_path = $this->getPackagePath($package); - $prepend_source_file = null; - $append_source_file = null; + $prepend_source_file = NULL; + $append_source_file = NULL; if (isset($metadata['prepend'])) { $prepend_source_file = ScaffoldFilePath::sourcePath($package_name, $package_path, $dest_rel_path, $metadata['prepend']); } diff --git a/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php b/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php index a5961c6b2f..a980e9d2bf 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php @@ -27,15 +27,13 @@ class ReplaceOp implements OperationInterface { protected $overwrite; /** - * Sets the relative path to the source. + * Constructs a ReplaceOp. * * @param \Drupal\Component\Scaffold\ScaffoldFilePath $sourcePath * The relative path to the source file. * @param bool $overwrite * Whether to allow this scaffold file to overwrite files already at * the destination. Defaults to TRUE. - * - * @return $this */ public function __construct(ScaffoldFilePath $sourcePath, $overwrite = TRUE) { $this->source = $sourcePath; diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php index 6bd8d8ab87..274ef39e45 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php @@ -40,9 +40,7 @@ public function testCoalateScaffoldFiles() { ]; $sut = new OperationCollection($fixtures->io()); // Test the system under test. - $sut->collateScaffoldFiles($file_mappings, $locationReplacements); - $resolved_file_mappings = $sut->fileMappings(); - $scaffold_list = $sut->scaffoldList(); + list($scaffold_list, $resolved_file_mappings) = $this->callProtected($sut, 'collateScaffoldFiles', [$file_mappings, $locationReplacements]); // Confirm that the keys of the output are the same as the keys of the input. $this->assertEquals(array_keys($file_mappings), array_keys($resolved_file_mappings)); // Also assert that we have the right ScaffoldFileInfo objects in the destination. @@ -88,4 +86,24 @@ protected function assertOverridden($project, $dest, $scaffold_list, $resolved_f $this->assertNotEquals($project, $scaffold_list[$dest]->packageName()); } + /** + * Uses reflection to call a protected method of an object. + * + * @param mixed $obj + * The object to inspect. + * @param string $methodName + * The name of the method to call + * @param array $args + * The arguments to pass to the protected method + * + * @return mixed + * The return value from the protected methos. + */ + protected function callProtected($obj, $methodName, $args = []) + { + $r = new \ReflectionMethod($obj, $methodName); + $r->setAccessible(true); + return $r->invokeArgs($obj, $args); + } + }