Skip to content

Commit

Permalink
[phar-io#233 phar-io#17]: Rethink configuration storage at project level
Browse files Browse the repository at this point in the history
- Read configuration from .phive and current working directory (phars + auth)
- Update skel command
- Add warning in output if both configuration exist
- Add project level repositories
- Copy phive.xml to .phive/phars.xml
  • Loading branch information
MacFJA committed May 10, 2020
1 parent 29173b8 commit 2f9c472
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 29 deletions.
8 changes: 8 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpab" version="^1.25.1" installed="1.25.9" location="./tools/phpab" copy="false"/>
<phar name="phpunit" version="^8.1" installed="8.5.3" location="./tools/phpunit" copy="false"/>
<phar name="phpstan" version="^0.11.5" installed="0.12.18" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="^3.4.10" installed="3.11.2" location="./tools/psalm" copy="false"/>
<phar name="php-cs-fixer" version="^2.15.3" installed="2.16.3" location="./tools/php-cs-fixer" copy="false"/>
</phive>
File renamed without changes.
9 changes: 6 additions & 3 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,21 +416,24 @@ private function getPhiveXmlConfig(bool $global): PhiveXmlConfig {
'https://phar.io/phive',
'phive'
),
new VersionConstraintParser()
new VersionConstraintParser(),
$this->getEnvironment()
);
}

private function getPhiveXmlConfigFileLocator(): PhiveXmlConfigFileLocator {
return new PhiveXmlConfigFileLocator(
$this->getEnvironment(),
$this->getConfig()
$this->getConfig(),
$this->getOutput()
);
}

private function getAuthXmlConfigFileLocator(): AuthXmlConfigFileLocator {
return new AuthXmlConfigFileLocator(
$this->getEnvironment(),
$this->getConfig()
$this->getConfig(),
$this->getOutput()
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/skel/SkelCommandConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public function allowOverwrite(): bool {

public function getDestination(): string {
if ($this->cliOptions->hasOption('auth')) {
return $this->workingDirectory . '/phive-auth.xml';
return $this->workingDirectory . '/.phive/auth.xml';
}

return $this->workingDirectory . '/phive.xml';
return $this->workingDirectory . '/.phive/phars.xml';
}

public function getTemplateFilename(): string {
if ($this->cliOptions->hasOption('auth')) {
return __DIR__ . '/../../../conf/phive-auth.skeleton.xml';
return __DIR__ . '/../../../conf/auth.skeleton.xml';
}

return __DIR__ . '/../../../conf/phive.skeleton.xml';
Expand Down
6 changes: 5 additions & 1 deletion src/services/resolver/RequestedPharResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public function getGitlabAliasResolver(): GitlabAliasResolver {
}

public function getLocalSourcesListFileLoader(): LocalSourcesListFileLoader {
return $this->factory->getLocalSourcesListFileLoader();
}

public function getProjectSourcesListFileLoader(): LocalSourcesListFileLoader {
return new LocalSourcesListFileLoader(
$this->factory->getConfig()->getHomeDirectory()->file('local.xml')
$this->factory->getConfig()->getWorkingDirectory()->child('.phive')->file('repositories.xml')
);
}
}
5 changes: 5 additions & 0 deletions src/services/resolver/strategy/AbstractResolvingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public function __construct(RequestedPharResolverFactory $factory) {
}

public function execute(RequestedPharResolverService $service): void {
// project repository
$service->addResolver(
$this->factory->getPharIoAliasResolver($this->factory->getProjectSourcesListFileLoader())
);

// github.com
$service->addResolver($this->factory->getGithubAliasResolver());

Expand Down
23 changes: 18 additions & 5 deletions src/shared/config/AuthXmlConfigFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@
namespace PharIo\Phive;

class AuthXmlConfigFileLocator {
private const FILENAME = 'phive-auth.xml';

/** @var Config */
private $config;

/** @var Environment */
private $environment;

public function __construct(Environment $environment, Config $config) {
/** @var Cli\Output */
private $output;

public function __construct(Environment $environment, Config $config, Cli\Output $output) {
$this->environment = $environment;
$this->config = $config;
$this->output = $output;
}

public function getFile(bool $global): \PharIo\FileSystem\Filename {
if ($global) {
return $this->config->getHomeDirectory()->file(self::FILENAME);
return $this->config->getHomeDirectory()->file('auth.xml');
}

$primary = $this->environment->getWorkingDirectory()->child('.phive')->file('auth.xml');
$fallback = $this->environment->getWorkingDirectory()->file('phive-auth.xml');

if ($primary->exists() && $fallback->exists()) {
$this->output->writeWarning('Both .phive/auth.xml and phive-auth.xml shouldn\'t be defined. Please prefer using .phive/auth.xml');
}

if (!$primary->exists() && $fallback->exists()) {
return $fallback;
}

return $this->environment->getWorkingDirectory()->file(self::FILENAME);
return $primary;
}
}
11 changes: 10 additions & 1 deletion src/shared/config/LocalPhiveXmlConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
namespace PharIo\Phive;

use PharIo\FileSystem\Filename;
use PharIo\Version\VersionConstraintParser;

class LocalPhiveXmlConfig extends PhiveXmlConfig {
/** @var Environment */
private $environment;

public function __construct(XmlFile $configFile, VersionConstraintParser $versionConstraintParser, Environment $environment) {
parent::__construct($configFile, $versionConstraintParser);
$this->environment = $environment;
}

protected function getLocation(InstalledPhar $installedPhar): Filename {
return $installedPhar->getLocation()->getRelativePathTo($this->getOwnDirectory());
return $installedPhar->getLocation()->getRelativePathTo($this->environment->getWorkingDirectory());
}
}
4 changes: 0 additions & 4 deletions src/shared/config/PhiveXmlConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ public function setTargetDirectory(Directory $directory): void {

abstract protected function getLocation(InstalledPhar $installedPhar): Filename;

protected function getOwnDirectory(): Directory {
return $this->configFile->getDirectory();
}

private function hasPharNode(string $name): bool {
return $this->getPharNode($name) !== null;
}
Expand Down
23 changes: 18 additions & 5 deletions src/shared/config/PhiveXmlConfigFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@
namespace PharIo\Phive;

class PhiveXmlConfigFileLocator {
public const FILENAME = 'phive.xml';

/** @var Config */
private $config;

/** @var Environment */
private $environment;

public function __construct(Environment $environment, Config $config) {
/** @var Cli\Output */
private $output;

public function __construct(Environment $environment, Config $config, Cli\Output $output) {
$this->environment = $environment;
$this->config = $config;
$this->output = $output;
}

public function getFile(bool $global): \PharIo\FileSystem\Filename {
if ($global) {
return $this->config->getHomeDirectory()->file(self::FILENAME);
return $this->config->getHomeDirectory()->file('phive.xml');
}

$primary = $this->environment->getWorkingDirectory()->child('.phive')->file('phars.xml');
$fallback = $this->environment->getWorkingDirectory()->file('phive.xml');

if ($primary->exists() && $fallback->exists()) {
$this->output->writeWarning('Both .phive/phars.xml and phive.xml shouldn\'t be defined. Please prefer using .phive/phars.xml');
}

if (!$primary->exists() && $fallback->exists()) {
return $fallback;
}

return $this->environment->getWorkingDirectory()->file(self::FILENAME);
return $primary;
}
}
6 changes: 3 additions & 3 deletions tests/unit/commands/skel/SkelCommandConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function allowOverwriteProvider() {
public function testGetDestination(): void {
$this->cliOptionsProphecy->hasOption('auth')->willReturn(false);
$config = new SkelCommandConfig($this->cliOptionsProphecy->reveal(), '/tmp/');
$this->assertEquals('/tmp/phive.xml', $config->getDestination());
$this->assertEquals('/tmp/.phive/phars.xml', $config->getDestination());

$this->cliOptionsProphecy->hasOption('auth')->willReturn(true);
$config = new SkelCommandConfig($this->cliOptionsProphecy->reveal(), '/tmp/');
$this->assertEquals('/tmp/phive-auth.xml', $config->getDestination());
$this->assertEquals('/tmp/.phive/auth.xml', $config->getDestination());
}

public function testGetTemplateFilename(): void {
Expand All @@ -53,7 +53,7 @@ public function testGetTemplateFilename(): void {

$this->cliOptionsProphecy->hasOption('auth')->willReturn(true);
$config = new SkelCommandConfig($this->cliOptionsProphecy->reveal(), '/tmp/');
$expected = \realpath(__DIR__ . '/../../../../conf/phive-auth.skeleton.xml');
$expected = \realpath(__DIR__ . '/../../../../conf/auth.skeleton.xml');
$actual = \realpath($config->getTemplateFilename());
$this->assertEquals($expected, $actual);
}
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/shared/config/AuthXmlConfigFileLocatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);
namespace PharIo\Phive;

use PharIo\FileSystem\Directory;
use PharIo\Phive\Cli\Output;
use PHPUnit\Framework\TestCase;

class AuthXmlConfigFileLocatorTest extends TestCase {
public function testWarnAboutDoubleConfigFile(): void {
$environmentMock = $this->getEnvironmentMock();
$environmentMock->method('getWorkingDirectory')
->willReturn(new Directory(__DIR__ . '/fixtures/doubleConfig'));

$outputMock = $this->getOutputMock();
$outputMock
->expects($this->once())
->method('writeWarning')
->with('Both .phive/auth.xml and phive-auth.xml shouldn\'t be defined. Please prefer using .phive/auth.xml');

$locator = new AuthXmlConfigFileLocator(
$environmentMock,
$this->getConfigMock(),
$outputMock
);

$locator->getFile(false);
}

/**
* @return Output|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getOutputMock() {
return $this->createMock(Output::class);
}

/**
* @return Environment|\PHPUnit_Framework_MockObject_MockObject
*/
private function getEnvironmentMock() {
return $this->createMock(Environment::class);
}

/**
* @return Config|\PHPUnit_Framework_MockObject_MockObject
*/
private function getConfigMock() {
return $this->createMock(Config::class);
}
}
15 changes: 11 additions & 4 deletions tests/unit/shared/config/LocalPhiveXmlConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testAddPharUpdatesExistingNode(): void {
$installedPhar->method('getName')->willReturn('phpunit');
$installedPhar->method('getLocation')->willReturn($filename);

$config = new LocalPhiveXmlConfig($configFile, $this->getVersionConstraintParserMock());
$config = new LocalPhiveXmlConfig($configFile, $this->getVersionConstraintParserMock(), $this->getEnvironmentMock());

$configFile->expects($this->once())->method('save');

Expand All @@ -69,7 +69,7 @@ public function testAddPharUpdatesExistingNode(): void {

public function testFindsPharNodesWithoutMatchingCase(): void {
$xmlFile = new XmlFile(new Filename(__DIR__ . '/fixtures/phive.xml'), 'https://phar.io/phive', 'phive');
$config = new LocalPhiveXmlConfig($xmlFile, new VersionConstraintParser());
$config = new LocalPhiveXmlConfig($xmlFile, new VersionConstraintParser(), $this->getEnvironmentMock());
$this->assertTrue($config->hasPhar('theseer/AUTOLOAD'));
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public function testAddPharCreatesNewNode(): void {
$phar = $this->getRequestedPharMock();
$phar->method('getAlias')->willReturn($alias);

$config = new LocalPhiveXmlConfig($configFile, $this->getVersionConstraintParserMock());
$config = new LocalPhiveXmlConfig($configFile, $this->getVersionConstraintParserMock(), $this->getEnvironmentMock());

$configFile->expects($this->once())->method('save');
$configFile->method('getDirectory')->willReturn($this->getDirectoryMock());
Expand Down Expand Up @@ -144,7 +144,7 @@ public function testGetPharsReturnsExpectedPhars(): void {
$configFile->method('query')->with('//phive:phar')
->willReturn($frag->childNodes);

$config = new LocalPhiveXmlConfig($configFile, $parserMock);
$config = new LocalPhiveXmlConfig($configFile, $parserMock, $this->getEnvironmentMock());
$expected = [
new ConfiguredPhar('https://example.com/phpunit-5.3.0.phar', new AnyVersionConstraint(), null, null, new PharUrl('https://example.com/phpunit-5.3.0.phar')),
new ConfiguredPhar('phpunit', new AnyVersionConstraint(), new Version('5.2.12'), new Filename(__DIR__ . '/fixtures/tools/phpunit')),
Expand Down Expand Up @@ -210,4 +210,11 @@ private function getInstalledPharMock() {
private function getFilenameMock() {
return $this->createMock(Filename::class);
}

/**
* @return Environment|\PHPUnit_Framework_MockObject_MockObject
*/
private function getEnvironmentMock() {
return $this->createMock(Environment::class);
}
}
49 changes: 49 additions & 0 deletions tests/unit/shared/config/PhiveXmlConfigFileLocatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);
namespace PharIo\Phive;

use PharIo\FileSystem\Directory;
use PharIo\Phive\Cli\Output;
use PHPUnit\Framework\TestCase;

class PhiveXmlConfigFileLocatorTest extends TestCase {
public function testWarnAboutDoubleConfigFile(): void {
$environmentMock = $this->getEnvironmentMock();
$environmentMock->method('getWorkingDirectory')
->willReturn(new Directory(__DIR__ . '/fixtures/doubleConfig'));

$outputMock = $this->getOutputMock();
$outputMock
->expects($this->once())
->method('writeWarning')
->with('Both .phive/phars.xml and phive.xml shouldn\'t be defined. Please prefer using .phive/phars.xml');

$locator = new PhiveXmlConfigFileLocator(
$environmentMock,
$this->getConfigMock(),
$outputMock
);

$locator->getFile(false);
}

/**
* @return Output|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getOutputMock() {
return $this->createMock(Output::class);
}

/**
* @return Environment|\PHPUnit_Framework_MockObject_MockObject
*/
private function getEnvironmentMock() {
return $this->createMock(Environment::class);
}

/**
* @return Config|\PHPUnit_Framework_MockObject_MockObject
*/
private function getConfigMock() {
return $this->createMock(Config::class);
}
}
18 changes: 18 additions & 0 deletions tests/unit/shared/config/fixtures/doubleConfig/.phive/auth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>
<!--
PHIVE AUTH CONFIGURATION SKELETON FILE
This file was automatically generated by PHIVE %%VERSION%% on %%DATE%%
-->
<auth xmlns="https://phar.io/phive-auth">

<!-- PHIVE AUTH CONFIGURATION -->

<!--
<domain host="api.github.com" type="token" credentials="ThisIsASecretToken" />
<domain host="example.com" type="basic" username="Aladdin" password="open sesame" />
<domain host="example.org" type="basic" credentials="QWxhZGRpbjpvcGVuIHNlc2FtZQ==" />
<domain host="gitlab.com" type="bearer" credentials="ThisIsAPrivateToken" />
-->

</auth>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="theseer/Autoload" version="^1.24.1" installed="1.24.1" location="./tools/phpab"/>
</phive>
Loading

0 comments on commit 2f9c472

Please sign in to comment.