Skip to content

Commit

Permalink
Automatic php version and framework dependency detection
Browse files Browse the repository at this point in the history
Introduces a abstraction to read data of composer.lock.
Use current php version and actual installed version of
magento/framework as version of dependencies for new
generated module.
  • Loading branch information
cmuench committed Jul 31, 2023
1 parent 2264b05 commit a101ea4
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 19 deletions.
6 changes: 3 additions & 3 deletions res/twig/dev/console/make/module/composer.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"proprietary"
],
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/framework": "100.0.*|100.1.*|101.0.*"
"php": "{{ php_version }}",
"magento/framework": "{{ magento_framework_version }}"
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"{{ namespace }}\\": ""
}
}}
}
}
12 changes: 12 additions & 0 deletions src/N98/Magento/Command/Developer/Console/MakeModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Module\ModuleListInterface;
use Magento\Framework\Module\Status;
use N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure;
use N98\Util\ComposerLock;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -30,6 +31,11 @@ class MakeModuleCommand extends AbstractGeneratorCommand
* @var string
*/
private $modulesBaseDir;
/**
*
* @var ComposerLock
*/
private $magentoComposerLock;

protected function configure()
{
Expand All @@ -55,6 +61,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->magentoComposerLock = new ComposerLock(
$this->getMagerunApplication()->getMagentoRootFolder()
);

$moduleName = new ModuleNameStructure($input->getArgument('modulename'));

$this->modulesBaseDir = $input->getOption('modules-base-dir');
Expand Down Expand Up @@ -151,6 +161,8 @@ private function createComposerFile(ModuleNameStructure $moduleName, WriteInterf
[
'vendor' => $moduleName->getVendorName(),
'module' => $moduleName->getShortModuleName(),
'php_version' => '~' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.0',
'magento_framework_version' => '~' . $this->magentoComposerLock->getPackageByName('magento/framework')->version,
'namespace' => str_replace('\\', '\\\\', $this->getModuleNamespace($moduleName->getFullModuleName())),
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\Filesystem\Io\File;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\ComposerLock;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -377,22 +378,8 @@ private function formatOutput(array $dependencies): string

private function loadProjectComposerPackagesByLockFile(string $magentoRootPath)
{
$packages = [];

$packagesConfig = json_decode(
\file_get_contents($magentoRootPath . '/composer.lock'),
false,
512,
JSON_THROW_ON_ERROR
);

if (isset($packagesConfig)) {
$packages = array_merge($packages, $packagesConfig->packages);
}

if (isset($packagesConfig->{'packages-dev'})) {
$packages = array_merge($packages, $packagesConfig->{'packages-dev'});
}
$composerLock = new ComposerLock($magentoRootPath);
$packages = $composerLock->getPackages();

$this->localInstalledPackagesFromLockFile = [];

Expand Down
86 changes: 86 additions & 0 deletions src/N98/Util/ComposerLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

declare(strict_types=1);

namespace N98\Util;

use Traversable;

class ComposerLock implements \IteratorAggregate
{
/**
* @var object
*/
private $composerJsonData;

/**
* @param $directoryOfComposerFile
*/
public function __construct($directoryOfComposerFile)
{
$this->directoryOfComposerFile = $directoryOfComposerFile;
}

private function load()
{
if (!empty($this->composerJsonData)) {
return;
}

if (file_exists($this->directoryOfComposerFile . '/composer.lock')) {
$this->composerJsonData = json_decode(
file_get_contents($this->directoryOfComposerFile . '/composer.lock'),
false,
512,
JSON_THROW_ON_ERROR
);
} else {
$this->composerJsonData = [];
}
}

public function getData()
{
$this->load();

return $this->composerJsonData;
}

public function getPackages(): array
{
$this->load();

$packages = [];

if (isset($this->composerJsonData->packages)) {
$packages = $this->composerJsonData->packages;
}

if (isset($packagesConfig->{'packages-dev'})) {
$packages = array_merge($packages, $this->composerJsonData->{'dev-packages'});
}

return $packages;
}

public function getPackageByName(string $packageName)
{
foreach ($this->getPackages() as $package) {
if ($package->name === $packageName) {
return $package;
}
}

return null;
}

public function getIterator(): Traversable
{
return new \ArrayIterator($this->getData());
}
}
36 changes: 36 additions & 0 deletions tests/N98/Util/ComposerLockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

declare(strict_types=1);

namespace N98\Util;

class ComposerLockTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @return void
*/
public function itShouldBeIteratable()
{
$lock = new ComposerLock(__DIR__ . '/_files/sample-project/composer');
$this->assertIsIterable($lock);
}

/**
* @test
* @return void
*/
public function itShouldReturnPackages()
{
$lock = new ComposerLock(__DIR__ . '/_files/sample-project/composer');
$this->assertIsArray($lock->getPackages());

$this->assertEquals('1.1.1', $lock->getPackageByName('psr/container')->version);
}

}

0 comments on commit a101ea4

Please sign in to comment.