-
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.
Refactored code, added to use default composer json and lock files if…
… possible (#4) Co-authored-by: Martins Rucevskis <[email protected]>
- Loading branch information
1 parent
a80589c
commit e79daaa
Showing
4 changed files
with
109 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace MartinsR\ComposerConstraintUpdater; | ||
|
||
class ComposerJsonFromLockBuilder | ||
{ | ||
public function __construct( | ||
private readonly string $composerJsonPath, | ||
private readonly string $composerLockPath | ||
){} | ||
|
||
public function versionsFromLock() | ||
{ | ||
$composerLockContents = file_get_contents($this->composerLockPath) | ||
?: throw new \Exception('Couldn\'t open composer lock file from ' . $this->composerLockPath); | ||
$composerJsonContents = file_get_contents($this->composerJsonPath) | ||
?: throw new \Exception('Couldn\'t open composer json file from ' . $this->composerJsonPath); | ||
|
||
$types = ['require', 'require-dev']; | ||
|
||
foreach ($types as $type) { | ||
foreach ($this->dependencies($composerJsonContents, $type) as $dependency => $version) { | ||
preg_match('#"name": ' . preg_quote($dependency) . ',\s+"version": "(.+)"#m', $composerLockContents, $match); | ||
|
||
if (isset($match[1])) { | ||
$lockVersion = str_contains($match[1],'dev')? $match[1] : '^'.$match[1]; | ||
$composerJsonContents = str_replace( | ||
$dependency . ': ' . $version, | ||
$dependency . ': "' . $lockVersion . '"', | ||
$composerJsonContents | ||
); | ||
} | ||
} | ||
} | ||
|
||
return $composerJsonContents; | ||
} | ||
|
||
private | ||
function dependencies(string $composerJsonContents, string $dependencyType): array | ||
{ | ||
preg_match('/"' . preg_quote($dependencyType) . '":\s+{([\s\S]+?)}/', $composerJsonContents, $dependencies); | ||
$dependencies = preg_split('/,/ms', $dependencies[1]); | ||
$composerDependencies = []; | ||
foreach ($dependencies as &$dependency) { | ||
$dependency = preg_replace('/\s+/ms', '', $dependency); | ||
$dependency = explode(':', $dependency); | ||
$composerDependencies[$dependency[0]] = $dependency[1]; | ||
} | ||
|
||
return $composerDependencies; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
<?php | ||
|
||
use MartinsR\ComposerConstraintUpdater\ConstraintUpdaterInitCommand; | ||
use MartinsR\ComposerConstraintUpdater\MajorConstraintUpdater; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ComposerUpdaterTest extends TestCase | ||
{ | ||
public function testComposerJsonUpdater(): void{ | ||
(new ConstraintUpdaterInitCommand())->replaceVersions(file_get_contents('C:\Users\martins.rucevskis\projects\ComposerConstraintUpdater\tests\resources\composerjson.txt'), ['laravel/framework' => '9.0.0']); | ||
(new MajorConstraintUpdater())->replaceVersions(file_get_contents('C:\Users\martins.rucevskis\projects\ComposerConstraintUpdater\tests\resources\composerjson.txt'), ['laravel/framework' => '9.0.0']); | ||
} | ||
|
||
public function testComposerJsonFromLockFile(): void{ | ||
(new ConstraintUpdaterInitCommand())->versionsFromLock('C:\Users\martins.rucevskis\projects\ComposerConstraintUpdater\tests\resources\composer.json.txt'); | ||
(new MajorConstraintUpdater())->versionsFromLock('C:\Users\martins.rucevskis\projects\ComposerConstraintUpdater\tests\resources\composer.json.txt'); | ||
} | ||
} |