Original file: src/Fsm/Support/DefinitionDiscoverer.php
The DefinitionDiscoverer class is a critical component of the FSM (Finite State Machine) package within the Laravel framework. Its primary role is to facilitate the discovery of FSM definitions within a given set of paths. The class utilizes Composer's ClassMapGenerator to dynamically locate and load classes that implement the FsmDefinition interface. By ensuring that only valid, concrete definitions are returned, it gathers all necessary FSM definitions for proper operation of the FSM system.
The discover method searches for classes within the specified paths that implement the FsmDefinition interface and are not abstract. It returns a unique list of these class names.
array<int, string> $paths: An array of directory paths to search for FSM definition classes. Each path should point to a valid directory.array<int, class-string<FsmDefinition>>: An array of unique class names that extend FsmDefinition. The classes returned are guaranteed to be concrete (non-abstract).The method processes the provided paths as follows:
$definitions is initialized to hold the found class names.is_dir(). If it's not a directory, the method skips to the next path.ClassMapGenerator::createMap($path), which returns an associative array mapping class names to their corresponding file paths.require_once. It checks if the class has already been loaded using class_exists().FsmDefinition and ensures it is not abstract by using ReflectionClass. If both criteria are met, the class is added to the $definitions array.try-catch block that handles any errors or exceptions that may arise during the class loading process (e.g., syntax errors, missing dependencies). In such cases, it simply continues to the next class.array_unique() and array_values(), which eliminates any duplicates and re-indexes the array numerically./**
* Discovers FSM definition classes from the provided paths.
*
* [@param](https://github.com/param) array<int, string> $paths
* [@return](https://github.com/return) array<int, class-string<FsmDefinition>>
*/
public static function discover(array $paths): array {
...
}
$paths = ['/path/to/first/directory', '/path/to/second/directory'];
$definitions = DefinitionDiscoverer::discover($paths);
In this example, the discover method will scan the given directories for FSM definition classes and return a list of the discovered classes.
By detailing the functionality of the discover method, developers will understand how the DefinitionDiscoverer class contributes to the overall architecture of the FSM package, ensuring robust and dynamic loading of FSM definitions at runtime.
How can I help you explore Laravel packages today?