-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller.php
168 lines (143 loc) · 3.34 KB
/
installer.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
new Installer;
/**
* Helps to run application on first time
*/
class Installer
{
private $foldersToChmod = array(
'temp/' => 0777,
'log/' => 0777,
);
/** @var array */
private $config = array();
/** @var string */
private $folderName;
/** @var string */
private $databaseName;
public function __construct()
{
$this->folderName = basename(getcwd());
$this->readDatabaseConfig();
$this->createDatabase();
$this->createConfig();
$this->setPermissions();
$this->warning("Please run migrations and 'composer update --dev --prefer-dist' if you're starting a new project.");
}
/**
* Reads database config from standard input
*/
private function readDatabaseConfig()
{
$values = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => $this->folderName,
);
foreach ($values as $field => $default)
{
if (!extension_loaded('readline')) {
echo 'Enter database ' . $field . ' [' . $default . ']: ';
$input = stream_get_line(STDIN, 1024, PHP_EOL);
} else {
$input = readline('Enter database ' . $field . ' [' . $default . ']: ');
}
if ($input === '')
{
$this->config['db'][$field] = $default;
}
else
{
$this->config['db'][$field] = $input;
}
}
}
/**
* Creates database with name based on folder
*/
private function createDatabase()
{
$connection = @mysqli_connect($this->config['db']['host'], $this->config['db']['username'], $this->config['db']['password']);
if (mysqli_connect_errno())
{
$this->error('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = 'CREATE DATABASE ' . $this->config['db']['database'];
if (!mysqli_query($connection, $sql))
{
$this->error('Error creating database: ' . mysqli_error($connection));
}
$this->success('Database created ...');
}
/**
* Sets permissions on specified folders.
*/
private function setPermissions()
{
foreach ($this->foldersToChmod as $folder => $permissions)
{
chmod(__DIR__ . '/' . $folder, $permissions);
}
$this->success('Permissions changed ...');
}
/**
* Copies example config and edits it accroding to credentials
*/
private function createConfig()
{
$configFilename = __DIR__ . '/app/config/config.local.neon';
copy(__DIR__ . '/app/config/config.local.example.neon', $configFilename);
$lines = file($configFilename);
foreach ($lines as $key => $line)
{
foreach (array('username', 'password', 'database') as $subject)
{
if (strpos($line, "\t" . $subject))
{
$lines[$key] = $this->deleteNewline($line, "\n") . ' ' . $this->config['db'][$subject] . "\n";
}
}
}
if (file_put_contents($configFilename, $lines))
{
$this->success('Local config file created ...');
}
}
/**
* Removes new line from string
* @param string
* @return string
*/
private function deleteNewline($string)
{
return trim($string, "\n");
}
/**
* Prints error message and dies.
* @param string
*/
private function error($string)
{
echo "\033[0;31m" . $string . "\033[0m\n";
exit(1);
}
/**
*
* Prints success message.
* @param string
*/
private function success($string)
{
echo "\033[0;32m" . $string . "\033[0m\n";
}
/**
*
* Prints warning message.
* @param string
*/
private function warning($string)
{
echo "\033[0;36m" . $string . "\033[0m\n";
}
}