Skip to content

Commit 29bc089

Browse files
committed
Look for composer.json in parent of registered module directory for sample data suggestions
This allows modules to follow the pds/skeleton standard and have their source code in a 'src' subdirectory of the repository
1 parent 9768fc6 commit 29bc089

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

app/code/Magento/SampleData/Model/Dependency.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ class Dependency
4848
* @param Filesystem $filesystem
4949
* @param PackageFactory $packageFactory
5050
* @param ComponentRegistrarInterface $componentRegistrar
51+
* @param Filesystem\Directory\ReadInterfaceFactory $directoryReadFactory
52+
* @throws \RuntimeException
5153
*/
5254
public function __construct(
5355
ComposerInformation $composerInformation,
5456
Filesystem $filesystem,
5557
PackageFactory $packageFactory,
5658
ComponentRegistrarInterface $componentRegistrar,
57-
\Magento\Framework\Filesystem\Directory\ReadInterfaceFactory $directoryReadFactory = null
59+
Filesystem\Directory\ReadInterfaceFactory $directoryReadFactory = null
5860
) {
5961
$this->composerInformation = $composerInformation;
6062
$this->packageFactory = $packageFactory;
@@ -69,6 +71,7 @@ public function __construct(
6971
* Retrieve list of sample data packages from suggests
7072
*
7173
* @return array
74+
* @throws \Magento\Framework\Exception\FileSystemException
7275
*/
7376
public function getSampleDataPackages()
7477
{
@@ -87,20 +90,13 @@ public function getSampleDataPackages()
8790
* Retrieve suggested sample data packages from modules composer.json
8891
*
8992
* @return array
93+
* @throws \Magento\Framework\Exception\FileSystemException
9094
*/
9195
protected function getSuggestsFromModules()
9296
{
9397
$suggests = [];
9498
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
95-
/** @var Filesystem\Directory\ReadInterface $directory */
96-
$directory = $this->directoryReadFactory->create(['path' => $moduleDir]);
97-
echo "path=$moduleDir\n";
98-
if (!$directory->isExist('composer.json') || !$directory->isReadable('composer.json')) {
99-
continue;
100-
}
101-
102-
/** @var Package $package */
103-
$package = $this->getModuleComposerPackage($directory);
99+
$package = $this->getModuleComposerPackage($moduleDir);
104100
$suggest = json_decode(json_encode($package->get('suggest')), true);
105101
if (!empty($suggest)) {
106102
$suggests += $suggest;
@@ -112,12 +108,26 @@ protected function getSuggestsFromModules()
112108
/**
113109
* Load package
114110
*
115-
* @param Filesystem\Directory\ReadInterface $directory
111+
* @param string $moduleDir
116112
* @return Package
117113
* @throws \Magento\Framework\Exception\FileSystemException
118114
*/
119-
protected function getModuleComposerPackage(Filesystem\Directory\ReadInterface $directory)
115+
private function getModuleComposerPackage($moduleDir): Package
120116
{
121-
return $this->packageFactory->create(['json' => json_decode($directory->readFile('composer.json'))]);
117+
/*
118+
* Also look in parent directory of registered module directory to allow modules to follow the pds/skeleton
119+
* standard and have their source code in a "src" subdirectory of the repository
120+
*
121+
* see: https://github.com/php-pds/skeleton
122+
*/
123+
foreach ([$moduleDir, $moduleDir . DIRECTORY_SEPARATOR . '..'] as $dir) {
124+
/** @var Filesystem\Directory\ReadInterface $directory */
125+
$directory = $this->directoryReadFactory->create(['path' => $dir]);
126+
if ($directory->isExist('composer.json') && $directory->isReadable('composer.json')) {
127+
/** @var Package $package */
128+
return $this->packageFactory->create(['json' => json_decode($directory->readFile('composer.json'))]);
129+
}
130+
}
131+
return $this->packageFactory->create(['json' => new \stdClass]);
122132
}
123133
}

app/code/Magento/SampleData/Test/Unit/Model/DependencyTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static function dataPackagesFromComposerSuggest()
7979
'app/code/LocalModule',
8080
'app/code/LocalModuleWithoutComposerJson',
8181
'vendor/company/module',
82+
'vendor/company2/module/src'
8283
],
8384
'composerJsonGenerator' => function(DependencyTest $test) {
8485
return [
@@ -98,6 +99,28 @@ public static function dataPackagesFromComposerSuggest()
9899
['name' => 'company/module', 'suggest' => ['company/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.0.0-beta']]
99100
)
100101
],
102+
[
103+
['path' => 'vendor/company2/module/src/..'],
104+
$test->stubComposerJsonReader(
105+
['name' => 'company2/module', 'suggest' => ['company2/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.10']]
106+
)
107+
],
108+
[
109+
['path' => 'vendor/company2/module/src'],
110+
$test->stubFileNotFoundReader()
111+
],
112+
[
113+
['path' => 'vendor/company/module/..'],
114+
$test->stubFileNotFoundReader()
115+
],
116+
[
117+
['path' => 'app/code/LocalModuleWithoutComposerJson/..'],
118+
$test->stubFileNotFoundReader()
119+
],
120+
[
121+
['path' => 'app/code/LocalModule/..'],
122+
$test->stubFileNotFoundReader()
123+
],
101124
];
102125
},
103126
'suggestions' => [
@@ -110,6 +133,7 @@ public static function dataPackagesFromComposerSuggest()
110133
'thirdparty/bar-sample-data' => '1.2.3',
111134
'local/module-sample-data' => '0.1.0',
112135
'company/module-sample-data' => '1.0.0-beta',
136+
'company2/module-sample-data' => '1.10',
113137
]
114138
]
115139
];

0 commit comments

Comments
 (0)