steelpush
steelpush

Reputation: 109

Over-ride a Symfony component class

I need to make alterations to this symfony class:

Symfony\Component\Translation\MessageCatalogue

Does anyone know how I can force Symfony to use my own variant of this class? I don't want to tamper with the actual core files, just in case.

Upvotes: 0

Views: 140

Answers (1)

Shira
Shira

Reputation: 6560

The MessageCatalogue class isn't a service registered in the dependency injection container so it cannot be overriden using the standard methods (class name parameter / compiler pass). It is used directly in the Symfony code, e.g. new MessageCatalogue().

If you really need to override it, you can do so by setting the class location explicitly in the class loader. Class maps have precedence over PSR-0/PSR-4 prefixes.

$loader = require __DIR__ . '/../vendor/autoload.php';

$loader->addClassMap([
    'Symfony\Component\Translation\MessageCatalogue' => 'path/to/your/override.php',
]);

The downsides are:

  • it's a bit hackish - I'd only do this as a last resort
  • you can't extend the original class, you have to re-implement it or copy-paste the code
  • you have to remember to update it if you update Symfony

Upvotes: 3

Related Questions