-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bradfeehan/features/modular-loader
Initial implementation of modular loader
- Loading branch information
Showing
34 changed files
with
1,275 additions
and
2 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,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(); | ||
} |
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,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; | ||
} | ||
} |
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,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'" | ||
); | ||
} | ||
} |
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,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); | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.