1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Taisiya\PropelBundle\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\EventDispatcher\Event; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
8
|
|
|
use PhpParser\ParserFactory; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use Taisiya\CoreBundle\Composer\ScriptHandler as CoreScriptHandler; |
11
|
|
|
use Taisiya\PropelBundle\Database\Schema; |
12
|
|
|
|
13
|
|
|
defined('TAISIYA_ROOT') || define('TAISIYA_ROOT', dirname(dirname(__DIR__))); |
14
|
|
|
|
15
|
|
|
class ScriptHandler extends CoreScriptHandler |
16
|
|
|
{ |
17
|
|
|
const EVENT_BUILD_PROPEL_SCHEMA = 'composer.build_propel_schema'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Event $event |
21
|
|
|
*/ |
22
|
|
|
public static function createPropelConfigFile(Event $event): void |
23
|
|
|
{ |
24
|
|
|
$settings = require TAISIYA_ROOT.'/app/config/settings.php'; |
25
|
|
|
|
26
|
|
|
if (empty($settings['propel'])) { |
27
|
|
|
$event->getIO()->writeError(' - <error>propel configuration is empty</error>'); |
|
|
|
|
28
|
|
|
} elseif (!file_put_contents(TAISIYA_ROOT.'/propel.php', "<?php\n\nreturn ".var_export($settings['propel'], true).";\n")) { |
29
|
|
|
$event->getIO()->writeError(' - <error>couldn\'t write a file: '.TAISIYA_ROOT.'/propel.php</error>'); |
|
|
|
|
30
|
|
|
} else { |
31
|
|
|
$event->getIO()->write(' - <info>writed to '.TAISIYA_ROOT.'/propel.php</info>'); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Event $event |
37
|
|
|
*/ |
38
|
|
|
public static function buildPropelSchema(Event $event): void |
39
|
|
|
{ |
40
|
|
|
$dispatcher = $event->getComposer()->getEventDispatcher(); |
|
|
|
|
41
|
|
|
$schema = new Schema(); |
42
|
|
|
$buildPropelSchemaEvent = new Event(self::EVENT_BUILD_PROPEL_SCHEMA, ['schema' => $schema]); |
43
|
|
|
|
44
|
|
|
$finder = (new Finder()) |
45
|
|
|
->in([TAISIYA_ROOT.'/vendor', TAISIYA_ROOT.'/src']) |
46
|
|
|
->name('BuildPropelSchemaSubscriber.php'); |
47
|
|
|
|
48
|
|
|
foreach ($finder as $file) { |
49
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
50
|
|
|
|
51
|
|
|
$stmts = $parser->parse(file_get_contents($file->getPathname())); |
52
|
|
|
if ($stmts === null) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var Namespace_ $namespace */ |
57
|
|
|
$namespace = self::findNodeByInstanceType($stmts, Namespace_::class); |
58
|
|
|
if ($namespace === null) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var Node\Stmt\Class_ $class */ |
63
|
|
|
$class = self::findNodeByInstanceType($namespace->stmts, Node\Stmt\Class_::class); |
64
|
|
|
if ($class->name !== 'BuildPropelSchemaSubscriber' || $class->isAbstract()) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$fullClassName = implode('\\', $namespace->name->parts).'\\'.$class->name; |
69
|
|
|
$dispatcher->addSubscriber(new $fullClassName()); |
70
|
|
|
|
71
|
|
|
$event->getIO()->write(' - added subscriber <info>'.$fullClassName.'</info>'); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$dispatcher->dispatch(self::EVENT_BUILD_PROPEL_SCHEMA, $buildPropelSchemaEvent); |
75
|
|
|
|
76
|
|
|
if (!file_put_contents(TAISIYA_ROOT.'/schema.xml', $schema->generateOutputXml())) { |
77
|
|
|
throw new \RuntimeException('Couldn\'t save schema to '.TAISIYA_ROOT.'/schema.xml'); |
78
|
|
|
} else { |
79
|
|
|
$event->getIO()->write(' - saved schema to <info>'.TAISIYA_ROOT.'/schema.xml</info>'); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $stmts |
85
|
|
|
* @param string $classname |
86
|
|
|
* |
87
|
|
|
* @return Node|null |
88
|
|
|
*/ |
89
|
|
|
private static function findNodeByInstanceType(array $stmts, $classname) |
90
|
|
|
{ |
91
|
|
|
foreach ($stmts as $node) { |
92
|
|
|
if ($node instanceof $classname) { |
93
|
|
|
return $node; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.