Download the PHP package objective-php/matcher without Composer
On this page you can find all versions of the php package objective-php/matcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download objective-php/matcher
More information about objective-php/matcher
Files in objective-php/matcher
Package matcher
Short Description Generic engine to match identifiers against simple yet powerful patterns
License GPL-3.0
Homepage https://github.com/objective-php/matcher
Informations about the package matcher
Objective PHP / Matcher
Library topic
Matcher is a simple but powerful key matching engine meant for components using key based identification (events handler, dependencies injector...)
The idea behind os to offer a flexible and performing way to match individual keys as well as subsets in a collection. The first use case is our own EventHandler component. We thought that using the Matcher to attach callbacks to events would lead to a much more uniform and flexible mechanism than any other event handling component.
Concept
Sections
The identifiers that Matcher can work with are supposed to be built by concatenating sections
, more or less as namespaces work:
Sections are, by default, separated by a dot. This is however configurable in the component itself. Keys are supposed to be lowered case, but that's not mandatory. Just a good practice to keep keys uniform.
Matcher is aware of the section concept, and will rely on it when dealing with wildcards (see next chapter).
Wildcards
When comparing an identifier to another, either can contain one or more wildcards to symbolize sections: a question mark (?) will replace one section or key, while a star (*) will replace any number of sections and/or key.
Alternatives
In some case, wildcards are kindof too flexible... to restrict matching pattern, you can also use alternatives by providing several values for a single section of the identifier using the square brackets ([ and ])
Usage
Now that you are aware of what both section and wildcards are, let's see some examples, using realistic event identifiers coming from an MVC workflow sample:
In the above example, you can see how the Matcher can help easily binding a callback to several events at once. And if you need to me bore selective, this is where alternatives can help you:
To see more example, please take a look at the unit tests!
Performance
The matching algorithm has been rewritten several times to reach more than decent performances. With dozen of thousands call looping on large identifiers stack (also several thousands entries, including wildcards), the total user time consumed was above 100ms... Given that this is really improbable that a real application make use of that many identifiers (at least, for events or services for instance), we consider that the matching engine is very transparent from a performance perspective.