forked from netz98/n98-magerun2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic php version and framework dependency detection
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
Showing
5 changed files
with
140 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |