1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taisiya\CoreBundle\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\Composer; |
6
|
|
|
use Composer\EventDispatcher\Event; |
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
9
|
|
|
use Taisiya\CoreBundle\App; |
10
|
|
|
use Taisiya\CoreBundle\Event\Composer\CommandEvent; |
11
|
|
|
use Taisiya\CoreBundle\Event\Composer\InstallerEvent; |
12
|
|
|
use Taisiya\CoreBundle\Event\Composer\PackageEvent; |
13
|
|
|
use Taisiya\CoreBundle\Event\Composer\PluginEvent; |
14
|
|
|
|
15
|
|
|
class ScriptHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Composer $composer |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
final protected static function getRootDir(Composer $composer): string |
23
|
|
|
{ |
24
|
|
|
if (!defined('TAISIYA_ROOT')) { |
25
|
|
|
define('TAISIYA_ROOT', dirname($composer->getConfig()->get('vendor-dir'))); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return TAISIYA_ROOT; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Event $event |
33
|
|
|
*/ |
34
|
|
|
final public static function runEvents(Event $event): void |
35
|
|
|
{ |
36
|
|
|
$rootDir = self::getRootDir($event->getComposer()); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$app = file_exists($rootDir.'/bootstrap.php') |
39
|
|
|
? require_once $rootDir.'/bootstrap.php' : |
40
|
|
|
new App(['settings' => require_once $rootDir.'/app/config/settings.php']); |
41
|
|
|
|
42
|
|
|
/** @var EventDispatcher $dispatcher */ |
43
|
|
|
$dispatcher = $app->getContainer()['event_dispatcher']; |
44
|
|
|
|
45
|
|
|
foreach (require_once $rootDir.'/var/cache/internal/events_subscribers.cache.php' as $subscriberClass) { |
46
|
|
|
$dispatcher->addSubscriber(new $subscriberClass()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (preg_match('/-cmd$/', $event->getName())) { |
50
|
|
|
$detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\CommandEvent\\'.Inflector::classify($event->getName()).'Event'; |
51
|
|
View Code Duplication |
} elseif (preg_match('/-dependencies-solving$/', $event->getName())) { |
|
|
|
|
52
|
|
|
$detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\InstallerEvent\\'.Inflector::classify($event->getName()).'Event'; |
53
|
|
|
} elseif (preg_match('/-package-/', $event->getName())) { |
54
|
|
|
$detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PackageEvent\\'.Inflector::classify($event->getName()).'Event'; |
55
|
|
View Code Duplication |
} elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) { |
|
|
|
|
56
|
|
|
$detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PluginEvent\\'.Inflector::classify($event->getName()).'Event'; |
57
|
|
|
} |
58
|
|
|
$detailedEvent = new $detailedEventClass($app); |
|
|
|
|
59
|
|
|
$dispatcher->dispatch($detailedEvent::NAME, $detailedEvent); |
60
|
|
|
|
61
|
|
|
if (preg_match('/-cmd$/', $event->getName())) { |
62
|
|
|
$commandEvent = new CommandEvent($app); |
63
|
|
|
$dispatcher->dispatch($commandEvent::NAME, $commandEvent); |
64
|
|
|
} elseif (preg_match('/-dependencies-solving$/', $event->getName())) { |
65
|
|
|
$installerEvent = new InstallerEvent($app); |
66
|
|
|
$dispatcher->dispatch($installerEvent::NAME, $installerEvent); |
67
|
|
|
} elseif (preg_match('/-package-/', $event->getName())) { |
68
|
|
|
$packageEvent = new PackageEvent($app); |
69
|
|
|
$dispatcher->dispatch($packageEvent::NAME, $packageEvent); |
70
|
|
|
} elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) { |
71
|
|
|
$pluginEvent = new PluginEvent($app); |
72
|
|
|
$dispatcher->dispatch($pluginEvent::NAME, $pluginEvent); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: