-
Notifications
You must be signed in to change notification settings - Fork 2.5k
abstract factory for configs reading keys from merged config #4860
Conversation
travis failed on php 5.3.3 build only, all others are good. i cant find the issue here :-( |
/** | ||
* @var string | ||
*/ | ||
protected $pattern = '#^(.*)\\\\Config$#i'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be done more performant using strrpos
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm honestly not sure about the regex.
I've seen a lot of modules not use their namespace for the module-specific config key, but instead a normalized version. As an example, ZfcUser uses the key "zfc-user"; I use "phly-blog", "phly-simple-page", etc. As such, using a namespace separator and class-like naming feels off.
It might make sense to have several regexes to check against, and allow injecting additional ones (developers could attach the abstract factory by instantiating it, injecting it, and then adding it to the ServiceManager). If so, I'd include the following:
#config[._-](.*)$#i
#^(.*)[\\\\._-]config$#i
That would likely capture the bulk of existing modules, allowing them to adopt the convention by either suffixing or prepending some verbiage ("config.", "config-", "config_", "-config", ".config", "_config", "\Config") to their existing keys based on the style they've already adopted.
Then, either add a setter or a constructor with an optional dependency that allows providing additional regex to use; these should be preferred over the defaults, so treat them like a stack when matching.
It will mean a little more work matching, but be a bit more flexible and allow for different naming styles.
Please add unit tests, @sarge-ch |
Thanks, added a TestCase, 3 Tests with 4 Assertionms. This ran though on travis: [concat] ZendTest/Config Allthough it seems this branch has 2 failing Tests in ZendTest/View. |
@sarge-ch I've fixed the failing tests on develop earlier today, which means I can review this without issue. |
return false; | ||
} | ||
|
||
$config = $serviceLocator->get('Config'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd do a check with has('Config')
prior to this, and return false if that service is not available:
if (!$serviceLocator->has('Config')) {
return false;
}
$config = $serviceLocator->get('Config');
That way no exception is thrown if the config service is unavailable, but the method will still correctly report it cannot create the service.
@weierophinney Many thanks for your feedback and thoughts. I've implemented your suggestions and wrote some tests accordingly. |
public function setPatterns($patterns) | ||
{ | ||
if ($patterns instanceof \Traversable) { | ||
$patterns = ArrayUtils::iteratorToArray($patterns); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since patterns are not a nested structure, you can just use iterator_to_array()
here; I'll make that change on merge.
@sarge-ch Looking good -- a bit more feedback to incorporate, and I think we may have a winner! |
@weierophinney Thanks Matthew, added things :) I could not really understand what you've meant with s/$this/self there ... however I'd be happy to change this as well as per your request. PHPStorm seems to only like $this a the @return - so I did not change it. |
|
} | ||
|
||
$config = $serviceLocator->get('Config'); | ||
$config = new Config($config[$key]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted in a previous comment -- don't cast this to a Config
object; otherwise, there may be issues with existing code that works with configuration and expects an array (the current Config
service is actually an array, not a Config
instance!).
Rgr, will correct with the next commit. Btw: But I might miss the bigger picture here. If you still see a useful benefit in having this returning arrays I am happy with it. |
abstract factory for configs reading keys from merged config
…tract-config-factory abstract factory for configs reading keys from merged config
this abstract factory can be activated in your service manager config like this:
it will factor a \Zend\Config\Config object for requested names like
where in this case the key
MyModule\Sub
has to exist in the merged Config.