Skip to content

Commit

Permalink
Boyscout configuration classes and remove faulty test
Browse files Browse the repository at this point in the history
  • Loading branch information
mvriel committed Sep 24, 2018
1 parent b41f33c commit 6cf2594
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 57 deletions.
12 changes: 11 additions & 1 deletion src/phpDocumentor/Application/Configuration/Configuration.php
Expand Up @@ -2,10 +2,20 @@

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Mike van Riel <mike.vanriel@naenius.com>
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace phpDocumentor\Application\Configuration;


final class Configuration extends \ArrayObject
{

Expand Down
Expand Up @@ -19,14 +19,15 @@
use phpDocumentor\Application\Configuration\Factory\Version3;
use phpDocumentor\DomainModel\Uri;
use RuntimeException;
use SimpleXMLElement;

/**
* The ConfigurationFactory converts the configuration xml from a Uri into an array.
*/
final class ConfigurationFactory
{
/** @var Strategy[] All strategies that are used by the ConfigurationFactory. */
/**
* @var Strategy[] All strategies that are used by the ConfigurationFactory.
*/
private $strategies = [];

/**
Expand All @@ -36,74 +37,81 @@ final class ConfigurationFactory
* @var callable[]
*/
private $middlewares = [];
private $defaultFiles = [];

/**
* Initializes the ConfigurationFactory.
*
* @param Strategy[] $strategies
* @param Strategy[]|iterable $strategies
* @param array|null $defaultFiles
*/
public function __construct(iterable $strategies)
public function __construct(iterable $strategies, array $defaultFiles = null)
{
if ($defaultFiles === null) {
$defaultFiles = [
getcwd() . '/phpdoc.xml',
getcwd() . '/phpdoc.dist.xml',
getcwd() . '/phpdoc.xml.dist'
];
}

foreach ($strategies as $strategy) {
$this->registerStrategy($strategy);
}
$this->defaultFiles = $defaultFiles;
}

/**
* Adds a middleware callback that allows the consumer to alter the configuration array when it is constructed.
*
* @param callable $middleware
*/
public function addMiddleware(callable $middleware): void
{
$this->middlewares[] = $middleware;
}

/**
* Converts the phpDocumentor configuration xml to an array.
*
* @param Uri $uri The location of the file to be loaded.
* Attempts to load a configuration from the default locations for phpDocumentor
*
* @return Configuration
* @throws RuntimeException if no matching strategy can be found.
*/
public function fromUri(Uri $uri): Configuration
public function fromDefaultLocations(): Configuration
{
$file = (string) $uri;

if (!file_exists($file)) {
throw new \InvalidArgumentException(sprintf('File %s could not be found', $file));
foreach ($this->defaultFiles as $file) {
try {
return $this->fromUri(new Uri($file));
} catch (\InvalidArgumentException $e) {
continue;
}
}

return new Configuration(
$this->applyMiddleware(
$this->extractConfigurationArray(new SimpleXMLElement($file, 0, true))
)
);
return new Configuration($this->applyMiddleware(Version3::buildDefault()));
}

/**
* Attempts to load a configuration from
* Converts the phpDocumentor configuration xml to an array.
*
* @param Uri $uri The location of the file to be loaded.
*
* @return Configuration
* @throws RuntimeException if no matching strategy can be found.
*/
public function fromDefaultLocations(): Configuration
public function fromUri(Uri $uri): Configuration
{
$files = [
'file://' . getcwd() . '/phpdoc.xml',
'file://' . getcwd() . '/phpdoc.dist.xml',
'file://' . getcwd() . '/phpdoc.xml.dist'
];
$filename = (string) $uri;

foreach ($files as $file) {
try {
return $this->fromUri(new Uri($file));
} catch (\InvalidArgumentException $e) {
continue;
}
if (!file_exists($filename)) {
throw new \InvalidArgumentException(sprintf('File %s could not be found', $filename));
}

return new Configuration($this->applyMiddleware(Version3::buildDefault()));
return new Configuration($this->applyMiddleware($this->fromFile($filename)));
}

/**
* Adds strategies that are used in the ConfigurationFactory.
*
* @param Strategy $strategy
*/
private function registerStrategy(Strategy $strategy): void
{
Expand All @@ -113,10 +121,15 @@ private function registerStrategy(Strategy $strategy): void
/**
* Converts the given XML structure into an array containing the configuration.
*
* @param string $file the (xml) file that we try to import.
*
* @return array a structure containing all configuration options.
*
* @throws RuntimeException
*/
private function extractConfigurationArray(SimpleXMLElement $xml): array
private function fromFile(string $file): array
{
$xml = new \SimpleXMLElement($file, 0, true);
foreach ($this->strategies as $strategy) {
if ($strategy->supports($xml) === true) {
return $strategy->convert($xml);
Expand All @@ -128,6 +141,10 @@ private function extractConfigurationArray(SimpleXMLElement $xml): array

/**
* Applies all middleware callbacks onto the configuration.
*
* @param array $configuration
*
* @return array
*/
private function applyMiddleware(array $configuration): array
{
Expand Down
23 changes: 0 additions & 23 deletions tests/unit/phpDocumentor/Application/Stage/ConfigureTest.php
Expand Up @@ -12,34 +12,11 @@

namespace phpDocumentor\Application\Stage;

use phpDocumentor\Application\Configuration\ConfigurationFactory;
use phpDocumentor\Application\Configuration\Factory\Version2;
use phpDocumentor\Application\Configuration\Factory\Version3;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \phpDocumentor\Application\Stage\Configure
*/
class ConfigureTest extends TestCase
{
/**
* @use \phpDocumentor\Application\Configuration\ConfigurationFactory;
* @use \phpDocumentor\Application\Configuration\Factory\Version3;
* @use \phpDocumentor\DomainModel\Uri;
*/
public function testInvokeOverridesConfig()
{
$configFactory = new ConfigurationFactory(
[
new Version2(),
new Version3(__DIR__ . '/../../../../../data/xsd/phpdoc.xsd')
]
);

$fixture = new Configure($configFactory);

$result = $fixture(['force' => true]);

$this->assertFalse($result['phpdocumentor']['use-cache']);
}
}

0 comments on commit 6cf2594

Please sign in to comment.