Injection

Sometimes you need to add some text content to an already existing file.

It is possible using injections.

Let's say we have a PHP class named CartRenderer like the following:

CartRenderer.php
<?php
class CartRenderer
{
}

After

In the following example we insert the interface implementation after the class name.

---
to: CartRenderer.php
inject: true
after: /^class .*/mi
---
{{ ' ' }}implements SingletonInterface

Before

This injection can also be done using the before setting.

This example inserts the final keyword before the class declaration.

---
to: CartRenderer.php
inject: true
before: /^class .*/mi
---
final{{ ' ' }}

Replace

This injection mode replace the actual match of the regex.

---
to: CartRenderer.php
inject: true
replace: /^final class/m
---
abstract class

Conditional injection

And last, we can skip any injections using the skip_if setting:

---
to: CartRenderer.php
inject: true
before: /^class .*/mi
skip_if: /^final class/mi
---
final{{ ' ' }}

Last updated