Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concatenate strings in config #203

Closed
mnapoli opened this issue Nov 12, 2014 · 4 comments
Closed

Concatenate strings in config #203

mnapoli opened this issue Nov 12, 2014 · 4 comments
Milestone

Comments

@mnapoli
Copy link
Member

mnapoli commented Nov 12, 2014

In config files we often need to concatenate strings, but by referencing other container entries (i.e. lazy concatenation).

For example:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\link('path.application') . '/tmp',
    'log.file' => DI\link('path.tmp') . '/app.log',
];

Of course this example doesn't work because DI\link(...) is not a string (it's a reference to a container entry). The only solution for now would be to use a closure which is overly verbose:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\factory(function (ContainerInterface $c) {
        return $c->get('path.application') . '/tmp';
    }),
    'log.file' => DI\factory(function (ContainerInterface $c) {
        return $c->get('path.tmp') . '/app.log';
    }),
];

Solutions

String concatenation definition

We could add a new string definition which would concatenate all parameters (lazily):

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\string(DI\link('path.application'), '/tmp'),
    'log.file' => DI\string(DI\link('path.tmp'), '/app.log'),
];

While it does look verbose, a PHP 5.6 example looks better:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => string(link('path.application'), '/tmp'),
    'log.file' => string(link('path.tmp'), '/app.log'),
];

String expression definition

We could have a string definition which allows to construct strings with expressions:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\string('{path.application}/tmp'),
    'log.file' => DI\string('{path.tmp}/app.log'),
];

Mix

We could also make DI\string() provide both behaviors. I don't like having a choice though, because it can be confusing, but maybe this can be an exception?

Other containers

  • Symfony:
log.file: "%path.tmp%/app.log"
  • Laravel: no support (except closures)
  • Aura: no support (except closures)
  • ZF2: no mention of string parameters in the Service Manager, maybe handled by another component?
@mnapoli mnapoli added this to the 5.0 milestone Nov 12, 2014
@diosmosis
Copy link

Is it possible to just do 'path.tmp' => '${path.application}/tmp', and have the container sort it out?

@mnapoli
Copy link
Member Author

mnapoli commented Nov 13, 2014

@diosmosis Nope :( because then there is no difference between simple strings and those that need processing.

@diosmosis
Copy link

Another idea: could combine the injectable string functionality w/ the factory function, eg, factory('${path.application}/tmp'), then you wouldn't have to define a new function at least. Of course, it's not really different from a new string function.

@mnapoli mnapoli mentioned this issue Dec 11, 2014
31 tasks
mnapoli added a commit that referenced this issue Jan 16, 2015
Implementation of #203: string definitions
@mnapoli
Copy link
Member Author

mnapoli commented Jan 16, 2015

Implemented in #224

Yay!

@mnapoli mnapoli closed this as completed Jan 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants