-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComposer.php
129 lines (117 loc) · 3.38 KB
/
Composer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* This Software is part of aryelgois/yasql-php and is provided "as is".
*
* @see LICENSE
*/
namespace aryelgois\YaSql;
use Composer\Script\Event;
/**
* Composer scripts for command line use
*
* Use it with Composer's run-script
*
* Paths are relative to the package root
*
* @author Aryel Mota Góis
* @license MIT
* @link https://www.github.com/aryelgois/yasql-php
*/
class Composer
{
/**
* Builds database schemas into a directory
*
* Arguments: (any order)
*
* config=path/to/config_file.yml (default 'config/databases.yml')
* output=path/to/output/ (default 'build/')
* vendor=vendor/package (multiple allowed)
*
* @param Event $event Composer run-script event
*/
public static function build(Event $event)
{
$args = $event->getArguments();
$config = null;
$output = null;
$vendors = [];
foreach ($args as $arg) {
$tokens = explode('=', $arg, 2);
if (count($tokens) === 1 && $arg[0] !== '-') {
throw new \InvalidArgumentException("Invalid argument '$arg'");
}
switch ($tokens[0]) {
case '-c':
$tokens[1] = '';
case 'config':
if ($config === null) {
$config = $tokens[1];
} else {
throw new \LogicException("Repeated 'config' argument");
}
break;
case 'output':
if ($output === null) {
$output = $tokens[1];
} else {
throw new \LogicException("Repeated 'output' argument");
}
break;
case 'vendor':
$vendors[] = $tokens[1];
break;
default:
$message = "Unknown argument '$tokens[0]' in '$arg'";
throw new \DomainException($message);
break;
}
}
if ($config === '' && count($vendors) === 0) {
echo "Nothing to do.\n";
die(1);
}
Controller::build(
$output ?? 'build/',
$config ?? 'config/databases.yml',
self::getVendorDir($event),
$vendors
);
}
/**
* Generates the SQL from a YASQL file
*
* Arguments:
*
* string $1 Path to YASQL file
* int $2 How many spaces per indentation level
*
* @param Event $event Composer run-script event
*/
public static function generate(Event $event)
{
$args = $event->getArguments();
if (empty($args)) {
echo "Usage:\n\n"
. " composer yasql-generate -- YASQL_FILE [INDENTATION]\n\n"
. "By default, INDENTATION is 2\n";
die(1);
}
echo Controller::generate(
file_get_contents($args[0]),
null,
$args[1] ?? 2
);
}
/**
* Gets Composer's vendor dir
*
* @param Event $event Composer run-script event
*
* @return string Path to Vendor Directory
*/
protected static function getVendorDir(Event $event)
{
return $event->getComposer()->getConfig()->get('vendor-dir');
}
}