-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add ComposerReader to read information from composer.json
This enables Tailor to automatically fetch the extension key if provided.
- Loading branch information
Showing
7 changed files
with
219 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 project - inspiring people to share! | ||
* (c) 2020 Oliver Bartsch & Benni Mack | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace TYPO3\Tailor\Exception; | ||
|
||
class InvalidComposerJsonException extends \InvalidArgumentException | ||
{ | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 project - inspiring people to share! | ||
* (c) 2020 Oliver Bartsch & Benni Mack | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace TYPO3\Tailor\Filesystem; | ||
|
||
use TYPO3\Tailor\Exception\InvalidComposerJsonException; | ||
|
||
/** | ||
* Reading information from composer.json | ||
*/ | ||
class ComposerReader | ||
{ | ||
/** @var array */ | ||
protected $composerSchema = []; | ||
|
||
public function __construct(string $path = '') | ||
{ | ||
$filename = rtrim($path ?: (string)(getcwd() ?: '.'), '/') . '/composer.json'; | ||
if (!file_exists($filename)) { | ||
return; | ||
} | ||
$content = @file_get_contents($filename); | ||
if ($content === false) { | ||
return; | ||
} | ||
$this->composerSchema = json_decode($content, true); | ||
if (!$this->composerSchema || $this->composerSchema === []) { | ||
throw new InvalidComposerJsonException('The composer.json found is invalid!', 1610442954); | ||
} | ||
} | ||
|
||
public function getExtensionKey(): string | ||
{ | ||
return $this->composerSchema['extra']['typo3/cms']['extension-key'] ?? ''; | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 project - inspiring people to share! | ||
* (c) 2020 Oliver Bartsch & Benni Mack | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace TYPO3\Tailor\Tests\Unit\Filesystem; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TYPO3\Tailor\Exception\InvalidComposerJsonException; | ||
use TYPO3\Tailor\Filesystem\ComposerReader; | ||
|
||
class ComposerReaderTest extends TestCase | ||
{ | ||
private const COMPOSER_FILE = 'tmp/composer.json'; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
mkdir('tmp'); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
unlink(self::COMPOSER_FILE); | ||
rmdir('tmp'); | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function throwsExceptionOnEmptyComposerJsonFile(): void | ||
{ | ||
$this->expectExceptionCode(1610442954); | ||
$this->expectException(InvalidComposerJsonException::class); | ||
file_put_contents(self::COMPOSER_FILE, ''); | ||
new ComposerReader('tmp'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function throwsExceptionOnInvalidComposerJsonFile(): void | ||
{ | ||
$this->expectExceptionCode(1610442954); | ||
$this->expectException(InvalidComposerJsonException::class); | ||
$composerContent = file_get_contents(__DIR__ . '/../Fixtures/EmConf/emconf_valid.php'); | ||
file_put_contents(self::COMPOSER_FILE, $composerContent); | ||
new ComposerReader('tmp'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function returnEmptyStringIfExtensionKeyNotGiven(): void | ||
{ | ||
$composerContent = file_get_contents(__DIR__ . '/../Fixtures/Composer/composer_no_extension_key.json'); | ||
file_put_contents(self::COMPOSER_FILE, $composerContent); | ||
$subject = new ComposerReader('tmp'); | ||
self::assertEmpty($subject->getExtensionKey()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function readCorrectExtensionKeyFromGivenComposerJsonFile(): void | ||
{ | ||
$composerContent = file_get_contents(__DIR__ . '/../Fixtures/Composer/composer_with_extension_key.json'); | ||
file_put_contents(self::COMPOSER_FILE, $composerContent); | ||
$subject = new ComposerReader('tmp'); | ||
self::assertSame('my-extension', $subject->getExtensionKey()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/Unit/Fixtures/Composer/composer_no_extension_key.json
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,20 @@ | ||
{ | ||
"name": "acme/my_extension", | ||
"type": "typo3-cms-extension", | ||
"description": "Great extension - everyone needs it", | ||
"authors": [ | ||
{ | ||
"name": "John Doe", | ||
"role": "Dev" | ||
} | ||
], | ||
"license": [ | ||
"GPL-2.0-or-later" | ||
], | ||
"require": { | ||
"typo3/cms-core": "^10.4" | ||
}, | ||
"replace": { | ||
"typo3-ter/news": "self.version" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Unit/Fixtures/Composer/composer_with_extension_key.json
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,25 @@ | ||
{ | ||
"name": "acme/my_extension", | ||
"type": "typo3-cms-extension", | ||
"description": "Great extension - everyone needs it", | ||
"authors": [ | ||
{ | ||
"name": "John Doe", | ||
"role": "Dev" | ||
} | ||
], | ||
"license": [ | ||
"GPL-2.0-or-later" | ||
], | ||
"require": { | ||
"typo3/cms-core": "^10.4" | ||
}, | ||
"replace": { | ||
"typo3-ter/news": "self.version" | ||
}, | ||
"extra": { | ||
"typo3/cms": { | ||
"extension-key": "my-extension" | ||
} | ||
} | ||
} |