Skip to content

Commit

Permalink
Merge pull request #1 from bradfeehan/features/modular-loader
Browse files Browse the repository at this point in the history
Initial implementation of modular loader
  • Loading branch information
bradfeehan committed May 5, 2014
2 parents 85d2e50 + 1a51f36 commit f494da6
Show file tree
Hide file tree
Showing 34 changed files with 1,275 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
filter:
paths:
- lib/*
tools:
php_analyzer:
config:
Expand All @@ -8,7 +11,7 @@ tools:
naming:
enabled: true
local_variable: '^[a-z][a-zA-Z0-9]*$'
abstract_class_name: ^Abstract|Factory$
abstract_class_name: ^.*$
utility_class_name: 'Utils?$'
constant_name: '^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$'
property_name: '^[a-z][a-zA-Z0-9]*$'
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
],
"require": {
"php": ">=5.3",
"guzzle/guzzle": "~3.0"
"guzzle/guzzle": "~3.0",
"symfony/yaml": "~2.4"
},
"require-dev": {
"phpunit/phpunit": "~3.7.0",
Expand Down
31 changes: 31 additions & 0 deletions lib/ConfigLoader/ConfigLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace BradFeehan\GuzzleModularServiceDescriptions\ConfigLoader;

use Guzzle\Service\ConfigLoaderInterface as GuzzleConfigLoaderInterface;

/**
* Loads raw configuration data from files with particular extensions
*
* This is a more specific form of Guzzle's ConfigLoaderInterface,
* requiring implementing classes to explicitly specify the file
* extensions that they support.
*
* Guzzle's ConfigLoaderInterface doesn't specify what type the config
* loader should return, so implementing classes typically return
* highly structured data (i.e. objects).
*
* This interface is stricter, in that it requires implementing classes
* to always return a primitive representing the contents of the loaded
* file.
*/
interface ConfigLoaderInterface extends GuzzleConfigLoaderInterface
{

/**
* Retrieves the file extensions that this config loader supports
*
* @return array<string> An array of strings
*/
public function getSupportedExtensions();
}
38 changes: 38 additions & 0 deletions lib/ConfigLoader/DefaultGuzzleConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace BradFeehan\GuzzleModularServiceDescriptions\ConfigLoader;

use Guzzle\Service\AbstractConfigLoader;

/**
* A config loader that delegates to Guzzle
*
* Guzzle supports JSON and PHP configuration files by default. This
* parser delegates to Guzzle's AbstractConfigLoader implementation to
* load configuration files of these types.
*/
class DefaultGuzzleConfigLoader extends AbstractConfigLoader implements ConfigLoaderInterface
{

/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
{
return array(
'js',
'json',
'php',
);
}

/**
* {@inheritdoc}
*
* Returns the configuration data untouched.
*/
protected function build($config, array $options)
{
return $config;
}
}
76 changes: 76 additions & 0 deletions lib/ConfigLoader/DelegatingConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace BradFeehan\GuzzleModularServiceDescriptions\ConfigLoader;

use InvalidArgumentException;

/**
* A configuration loader that delegates to other config loaders
*/
class DelegatingConfigLoader implements ConfigLoaderInterface
{

/**
* Stores the config loaders to delegate to
*
* @var array
*/
private $loaders = array();


/**
* Creates a new instance for a given set of loaders
*
* @param array $loaders The configuration loaders to delegate to
*/
public function __construct(array $loaders = array())
{
$this->loaders = $loaders;
}

/**
* Retrieves the loaders that this loader delegates to
*
* @return array
*/
public function getLoaders()
{
return $this->loaders;
}

/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
{
$extensions = array();

foreach ($this->getLoaders() as $loader) {
$extensions = array_merge(
$extensions,
$loader->getSupportedExtensions()
);
}

return array_unique($extensions);
}

/**
* {@inheritdoc}
*/
public function load($config, array $options = array())
{
$extension = pathinfo($config, PATHINFO_EXTENSION);

foreach ($this->getLoaders() as $loader) {
if (in_array($extension, $loader->getSupportedExtensions())) {
return $loader->load($config, $options);
}
}

throw new InvalidArgumentException(
"Couldn't load configuration file '$config': No " .
"configuration loader found for extension '$extension'"
);
}
}
39 changes: 39 additions & 0 deletions lib/ConfigLoader/FilesystemConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace BradFeehan\GuzzleModularServiceDescriptions\ConfigLoader;

/**
* A configuration loader that loads files from disk
*/
abstract class FilesystemConfigLoader implements ConfigLoaderInterface
{

/**
* {@inheritdoc}
*/
public function load($config, array $options = array())
{
return $this->parse($this->getFileContent($config));
}

/**
* Loads the raw data from a file
*
* @param string $filename The name of the file to load data from
*
* @return string The content of the file
*/
protected function getFileContent($filename)
{
return file_get_contents($filename);
}

/**
* Parses the raw data from the file into structured data
*
* @param string $data The content of the file
*
* @return mixed
*/
abstract protected function parse($data);
}
31 changes: 31 additions & 0 deletions lib/ConfigLoader/PlainTextConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace BradFeehan\GuzzleModularServiceDescriptions\ConfigLoader;

/**
* A configuration loader that loads plain text files
*
* This configuration loader returns the entire content of the file
* as a string. Files should be named with a .txt extension.
*/
class PlainTextConfigLoader extends FilesystemConfigLoader
{

/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
{
return array(
'txt',
);
}

/**
* {@inheritdoc}
*/
protected function parse($data)
{
return rtrim($data, PHP_EOL);
}
}
31 changes: 31 additions & 0 deletions lib/ConfigLoader/YamlConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace BradFeehan\GuzzleModularServiceDescriptions\ConfigLoader;

use Symfony\Component\Yaml\Yaml;

/**
* A configuration loader that loads YAML files
*/
class YamlConfigLoader extends FilesystemConfigLoader
{

/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
{
return array(
'yaml',
'yml',
);
}

/**
* {@inheritdoc}
*/
protected function parse($data)
{
return Yaml::parse($data);
}
}
Loading

0 comments on commit f494da6

Please sign in to comment.