1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Taisiya\CoreBundle\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\EventDispatcher\Event; |
6
|
|
|
use Doctrine\Common\Inflector\Inflector; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
10
|
|
|
use Taisiya\CoreBundle\App; |
11
|
|
|
use Taisiya\CoreBundle\Event\Composer\CommandEvent; |
12
|
|
|
use Taisiya\CoreBundle\Event\Composer\InstallerEvent; |
13
|
|
|
use Taisiya\CoreBundle\Event\Composer\PackageEvent; |
14
|
|
|
use Taisiya\CoreBundle\Event\Composer\PluginEvent; |
15
|
|
|
|
16
|
|
|
defined('TAISIYA_ROOT') || define('TAISIYA_ROOT', dirname(dirname(__DIR__))); |
17
|
|
|
|
18
|
|
|
class ScriptHandler |
19
|
|
|
{ |
20
|
|
|
const EVENT_EXECUTE = 'composer.handler.execute'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Event $event |
24
|
|
|
*/ |
25
|
|
|
final public static function runCommandEvents(Event $event): void |
26
|
|
|
{ |
27
|
|
|
$app = file_exists(TAISIYA_ROOT.'/bootstrap.php') |
28
|
|
|
? require_once TAISIYA_ROOT.'/bootstrap.php': |
29
|
|
|
new App(); |
30
|
|
|
|
31
|
|
|
/** @var EventDispatcher $dispatcher */ |
32
|
|
|
$dispatcher = $app->getContainer()['event_dispatcher']; |
33
|
|
|
|
34
|
|
|
$eventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\'.Inflector::classify($event->getName()).'Event'; |
35
|
|
|
$detailedEvent = new $eventClass(); |
36
|
|
|
$dispatcher->dispatch($detailedEvent::NAME, $detailedEvent); |
37
|
|
|
|
38
|
|
|
if (preg_match('/-cmd$/', $event->getName())) { |
39
|
|
|
$commandEvent = new CommandEvent(); |
40
|
|
|
$dispatcher->dispatch($commandEvent::NAME, $commandEvent); |
41
|
|
|
} elseif (preg_match('/-dependencies-solving$/', $event->getName())) { |
42
|
|
|
$installerEvent = new InstallerEvent(); |
43
|
|
|
$dispatcher->dispatch($installerEvent::NAME, $installerEvent); |
44
|
|
|
} elseif (preg_match('/-package-/', $event->getName())) { |
45
|
|
|
$packageEvent = new PackageEvent(); |
46
|
|
|
$dispatcher->dispatch($packageEvent::NAME, $packageEvent); |
47
|
|
|
} elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) { |
48
|
|
|
$pluginEvent = new PluginEvent(); |
49
|
|
|
$dispatcher->dispatch($pluginEvent::NAME, $pluginEvent); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.