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 step ( string $parentClassName, string $stepName ) : Step |
39
|
|
|
{ |
40
|
|
|
$parentClassShortName = ""; |
|
|
|
|
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$reflection = new ReflectionClass( $parentClassName ); |
44
|
|
|
|
45
|
|
|
$parentClassShortName = $reflection->getShortName(); |
46
|
|
|
} catch ( ReflectionException $reflectionException ) { |
47
|
|
|
$errorMessage = sprintf( "Something went wrong while trying to get the short class name: %s", $reflectionException->getMessage() ); |
48
|
|
|
throw new SourceWatcherException( $errorMessage ); |
49
|
|
|
} catch ( Exception $exception ) { |
|
|
|
|
50
|
|
|
$errorMessage = sprintf( "Something unexpected went wrong while trying to get the short class name: %s", $exception->getMessage() ); |
51
|
|
|
throw new SourceWatcherException( $errorMessage ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$baseNameSpace = "Coco\\SourceWatcher\\Core"; |
55
|
|
|
|
56
|
|
|
$packages = [ "Extractor" => sprintf( "%s\\%s", $baseNameSpace, "Extractors" ), "Transformer" => sprintf( "%s\\%s", $baseNameSpace, "Transformers" ), "Loader" => sprintf( "%s\\%s", $baseNameSpace, "Loaders" ) ]; |
57
|
|
|
|
58
|
|
|
$subPackage = $packages[$parentClassShortName]; |
59
|
|
|
|
60
|
|
|
$textUtils = new TextUtils(); |
61
|
|
|
|
62
|
|
|
$step = null; |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$temporaryClassName = sprintf( "%s_%s", $stepName, $parentClassShortName ); |
66
|
|
|
|
67
|
|
|
$pascalCaseClassName = $textUtils->textToPascalCase( $temporaryClassName ); |
68
|
|
|
|
69
|
|
|
$fullyQualifiedClassName = sprintf( "%s\\%s", $subPackage, $pascalCaseClassName ); |
70
|
|
|
|
71
|
|
|
if ( class_exists( $fullyQualifiedClassName ) ) { |
72
|
|
|
$step = new $fullyQualifiedClassName(); |
73
|
|
|
} |
74
|
|
|
} catch ( Exception $exception ) { |
75
|
|
|
$errorMessage = sprintf( "Something unexpected went wrong while trying to load the step: %s", $exception->getMessage() ); |
76
|
|
|
throw new SourceWatcherException( $errorMessage ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $step; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|