Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Last active August 29, 2015 14:06
Show Gist options
  • Save mnapoli/12a61856addbb67f4563 to your computer and use it in GitHub Desktop.
Save mnapoli/12a61856addbb67f4563 to your computer and use it in GitHub Desktop.

Related tickets:

Definitions

  • Class
  • Factory
  • Value
  • Alias
  • Array (new)
  • String/concat (new)?

Definition extensions

  • Callback/Factory
  • Array
  • Class?
  • Decorate (composition)?

For reference, in Symfony decoration is done that way: symfony/symfony#9003

Example

Base config:

<?php
return [
    Foo::class => object()
        ->constructorParam('param1', 0)
        ->constructorParam('param2', 1),
    
    'logger.handlers' => [
        link(StdOutHandler::class),
    ],
    
    // String
    'path.tmp' => '/tmp',
    'path.log' => string(link('path.tmp'), '/app.log'),
];

Factory extension:

<?php
return [
    Foo::class => extend()->factory(function (Foo $foo, ContainerInterface $container) {
        $foo->setStuff(true);
        return $foo;
    }),
];

Array extension:

<?php
return [
    'logger.handlers' => extend()->array([
        link(PaperTrailHandler::class),
    ]),
];

Class extension:

<?php
return [
    Foo::class => extend()->object()
        ->constructorParam('param2', 10),
];

What about chaining?

<?php
return [
    Foo::class => object(Bar::class)
        ->constructor(original(Foo::class)),
];

Or more simply (and probably saner):

<?php
return [
    Foo::class => object(),
    Bar::class => object()
        ->constructor(link(Foo::class)),
    SomeInterface::class => link(Bar::class),
];
  1. Map an interface to an implementation
Psr\Log\LoggerInterface = Monolog\Logger
  1. Map an interface to a PHP function that returns the instance
Psr\Log\LoggerInterface = function () {
    return new Monolog\Logger();
}
  1. Define an array of objects
log_handlers = [
    Monolog\Handler\StreamHandler,
    Monolog\Handler\NewRelicHandler,
]
  1. Add new entries to that array
log_handlers[] = Monolog\Handler\DebugBarHandler
  1. Override an array entry
database[host] = 'localhost'
  1. Decorate an object
Psr\Log\LoggerInterface = extend(Psr\Log\LoggerInterface, function (Psr\Log\LoggerInterface $logger) {
    return new LoggerDecorator($logger);
})
  1. Define an object
My\Service = new('param1', 'param2')
    ->setFormatter(get(Formatter))
  1. Extend an object definition
My\Service = 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment