This repository has been archived by the owner on Jul 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 94
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 #200 from KiwiJuicer/implement-zend-component-inst…
…aller-support Implement zend component installer support by adding ConfigProvider
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
namespace TwbBundle; | ||
|
||
/** | ||
* The configuration provider for the TwbBundle module | ||
* | ||
* @see https://docs.zendframework.com/zend-component-installer/ | ||
*/ | ||
class ConfigProvider | ||
{ | ||
/** | ||
* Path to the module config | ||
* | ||
* @const string | ||
*/ | ||
const MODULE_CONFIG_PATH = __DIR__ . '/../../config/module.config.php'; | ||
|
||
/** | ||
* The module config ZF2/3 | ||
* | ||
* @var array | ||
*/ | ||
protected $moduleConfig; | ||
|
||
/** | ||
* Returns the configuration array | ||
* | ||
* To add a bit of a structure, each section is defined in a separate | ||
* method which returns an array with its configuration. | ||
* | ||
* @return array | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __invoke() | ||
{ | ||
if (!file_exists(self::MODULE_CONFIG_PATH)) { | ||
throw new \InvalidArgumentException('Wrong path to module config file. File or directory does not exist: ' . self::MODULE_CONFIG_PATH); | ||
} | ||
|
||
$this->moduleConfig = require self::MODULE_CONFIG_PATH; | ||
|
||
return [ | ||
'twbbundle' => $this->getTwbBundleOptions(), | ||
'dependencies' => $this->getDependencies(), | ||
'view_helpers' => $this->getViewHelpers() | ||
]; | ||
} | ||
|
||
/** | ||
* Returns twb bundle options | ||
* | ||
* @return array | ||
*/ | ||
protected function getTwbBundleOptions() | ||
{ | ||
return array_key_exists('twbbundle', $this->moduleConfig) ? $this->moduleConfig['twbbundle'] : []; | ||
} | ||
|
||
/** | ||
* Returns dependencies (former server_manager) | ||
* | ||
* @return array | ||
*/ | ||
protected function getDependencies() | ||
{ | ||
return array_key_exists('service_manager', $this->moduleConfig) ? $this->moduleConfig['service_manager'] : []; | ||
} | ||
|
||
/** | ||
* Returns view helpers | ||
* | ||
* @return array | ||
*/ | ||
protected function getViewHelpers() | ||
{ | ||
return array_key_exists('view_helpers', $this->moduleConfig) ? $this->moduleConfig['view_helpers'] : []; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
namespace TwbBundleTest; | ||
|
||
use TwbBundle\ConfigProvider; | ||
|
||
/** | ||
* Config Provider Test | ||
* | ||
* @package TwbBundleTest | ||
*/ | ||
class ConfigProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* The zend-component config provider | ||
* | ||
* @var \TwbBundle\ConfigProvider | ||
*/ | ||
protected $configProvider; | ||
|
||
/** | ||
* @see \PHPUnit_Framework_TestCase::setUp() | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->configProvider = new ConfigProvider(); | ||
} | ||
|
||
/** | ||
* Tests valid return values | ||
*/ | ||
public function testInvokeReturnValues() | ||
{ | ||
$config = $this->configProvider->__invoke(); | ||
|
||
$this->assertArrayHasKey('twbbundle', $config); | ||
$this->assertArrayHasKey('dependencies', $config); | ||
$this->assertArrayHasKey('view_helpers', $config); | ||
|
||
$this->assertNotEmpty($config['twbbundle']); | ||
$this->assertNotEmpty($config['dependencies']); | ||
$this->assertNotEmpty($config['view_helpers']); | ||
} | ||
} |