1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Utils\TextUtils; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class StepLoader |
11
|
|
|
* @package Coco\SourceWatcher\Core |
12
|
|
|
*/ |
13
|
|
|
class StepLoader |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var StepLoader |
17
|
|
|
*/ |
18
|
|
|
private static StepLoader $instance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return StepLoader |
22
|
|
|
*/ |
23
|
|
|
public static function getInstance () : StepLoader |
24
|
|
|
{ |
25
|
|
|
if ( is_null( static::$instance ) ) { |
|
|
|
|
26
|
|
|
static::$instance = new static; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return static::$instance; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $parentClassName |
34
|
|
|
* @param string $stepName |
35
|
|
|
* @return Step |
36
|
|
|
* @throws SourceWatcherException |
37
|
|
|
*/ |
38
|
|
|
public function getStep ( string $parentClassName, string $stepName ) : ?Step |
39
|
|
|
{ |
40
|
|
|
if ( empty( $parentClassName ) ) { |
41
|
|
|
throw new SourceWatcherException( "The parent class name must be provided." ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ( empty( $stepName ) ) { |
45
|
|
|
throw new SourceWatcherException( "The step name must be provided." ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$reflection = new ReflectionClass( $parentClassName ); |
50
|
|
|
|
51
|
|
|
$parentClassShortName = $reflection->getShortName(); |
52
|
|
|
} catch ( ReflectionException $reflectionException ) { |
53
|
|
|
$errorMessage = sprintf( "Something went wrong while trying to get the short class name: %s", $reflectionException->getMessage() ); |
54
|
|
|
throw new SourceWatcherException( $errorMessage ); |
55
|
|
|
} catch ( Exception $exception ) { |
|
|
|
|
56
|
|
|
$errorMessage = sprintf( "Something unexpected went wrong while trying to get the short class name: %s", $exception->getMessage() ); |
57
|
|
|
throw new SourceWatcherException( $errorMessage ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$baseNameSpace = "Coco\\SourceWatcher\\Core"; |
61
|
|
|
|
62
|
|
|
$packages = [ "Extractor" => sprintf( "%s\\%s", $baseNameSpace, "Extractors" ), "Transformer" => sprintf( "%s\\%s", $baseNameSpace, "Transformers" ), "Loader" => sprintf( "%s\\%s", $baseNameSpace, "Loaders" ) ]; |
63
|
|
|
|
64
|
|
|
$subPackage = $packages[$parentClassShortName]; |
65
|
|
|
|
66
|
|
|
$textUtils = new TextUtils(); |
67
|
|
|
|
68
|
|
|
$step = null; |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$temporaryClassName = sprintf( "%s_%s", $stepName, $parentClassShortName ); |
72
|
|
|
|
73
|
|
|
$pascalCaseClassName = $textUtils->textToPascalCase( $temporaryClassName ); |
74
|
|
|
|
75
|
|
|
$fullyQualifiedClassName = sprintf( "%s\\%s", $subPackage, $pascalCaseClassName ); |
76
|
|
|
|
77
|
|
|
if ( class_exists( $fullyQualifiedClassName ) ) { |
78
|
|
|
$step = new $fullyQualifiedClassName(); |
79
|
|
|
} |
80
|
|
|
} catch ( Exception $exception ) { |
81
|
|
|
$errorMessage = sprintf( "Something unexpected went wrong while trying to load the step: %s", $exception->getMessage() ); |
82
|
|
|
throw new SourceWatcherException( $errorMessage ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $step; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|