forked from ccampbell/sonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·283 lines (233 loc) · 12.4 KB
/
install
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env php
<?php
/**
* installer for sonic
*/
use \Sonic\Util;
include 'lib/Sonic/Util.php';
/**
* outputs an error message and usage instructions
*
* @param string $message
* @return void
*/
function usage_and_exit($message)
{
echo $message,"\n";
echo "./install /path/to/install/to AppName","\n\n";
echo "optional arguments","\n";
echo "--verbose shows verbose output","\n";
echo "--core-only install only the core framework","\n";
echo "--test-framework include the unit test framework in this installation","\n";
echo "--symlink use a symlink to this library","\n";
echo "--with-jquery copies the latest jquery into the project","\n";
echo "--help show this menu","\n";
exit(1);
}
/**
* outputs a message
*
* @param string $message
* @param bool $verbose_only
* @return void
*/
function output($message, $verbose_only = false)
{
if ($verbose_only && !in_array('--verbose', $_SERVER['argv'])) {
return;
}
echo $message,"\n";
}
/**
* creates a file
*
* @param string $path
* @param string $contents
* @return void
*/
function createFile($path, $contents)
{
file_put_contents($path, $contents . "\n");
}
/**
* creates a directory
*
* @param string $path
* @param bool $merge is this part of an installation to merge into an existing directory?
* @param bool $ignore_during_merge do not attempt to override existing directory during merge
* @return void
*/
function createDirectory($path, $merge = false, $ignore_during_merge = false)
{
// if this is a merge and the directory already exists and this
// directory can be ignored then do nothing
if ($merge && is_dir($path) && $ignore_during_merge) {
return;
}
// default to create this directory
$create = true;
// if this is a merge and the directory already exists prompt the user
// to overwrite the existing directory
if ($merge && is_dir($path)) {
output('there is already a directory at: ' . $path);
output('would you like to overwrite it? (y or n)');
$yes = array('Y', 'y');
$create = in_array(strtolower(trim(fgets(STDIN))), $yes);
if ($create) {
Util::removeDir($path);
}
}
if ($create) {
mkdir($path);
}
}
$args = $_SERVER['argv'];
if (in_array('--help', $args)) {
usage_and_exit('usage');
}
if (!isset($args[1])) {
usage_and_exit('missing path');
}
if (!isset($args[2])) {
usage_and_exit('missing app name');
}
$INSTALL_PATH = $_SERVER['argv'][1];
$APP_NAME = $_SERVER['argv'][2];
$LC_APP_NAME = strtolower($APP_NAME);
$VERBOSE = in_array('--verbose', $args);
$ALL_FILES = !in_array('--core-only', $args);
$UNIT_TEST_FRAMEWORK = in_array('--test-framework', $args);
$SYMLINK = in_array('--symlink', $args);
$WITH_JQUERY = in_array('--with-jquery', $args);
$merge = $overwrite = false;
if (is_dir($INSTALL_PATH)) {
output('there is already a directory at: ' . $INSTALL_PATH);
output('would you like to merge into it? (y or n)');
$yes = array('Y', 'y');
$merge = in_array(strtolower(trim(fgets(STDIN))), $yes);
if (!$merge) {
output('would you like to overwrite it? (y or n)');
$overwrite = in_array(strtolower(trim(fgets(STDIN))), $yes);
}
// nothing to do exit
if (!$merge && !$overwrite) {
exit(0);
}
if ($overwrite) {
Util::removeDir(realpath($INSTALL_PATH));
}
}
// create all the directories and files we need
// if this isn't a merge then create the new directory
if (!$merge) {
output('creating new directory at ' . $INSTALL_PATH);
mkdir($INSTALL_PATH);
}
output('creating config directory', true);
createDirectory($INSTALL_PATH . '/configs', $merge);
output('creating controllers directory', true);
createDirectory($INSTALL_PATH . '/controllers', $merge);
output('creating libs directory', true);
createDirectory($INSTALL_PATH . '/libs', $merge);
output('creating public_html directory', true);
createDirectory($INSTALL_PATH . '/public_html', $merge);
if ($ALL_FILES) {
output('creating util directory', true);
createDirectory($INSTALL_PATH . '/util', $merge);
}
output('creating views directory', true);
createDirectory($INSTALL_PATH . '/views', $merge);
output('creating layouts directory', true);
createDirectory($INSTALL_PATH . '/views/layouts', $merge, true);
output('creating main views directory', true);
createDirectory($INSTALL_PATH . '/views/main', $merge, true);
if ($ALL_FILES) {
output('creating configs/app.ini', true);
$contents = "[global]\nurls[www] = \"http://www.$LC_APP_NAME.com\"\nurls[static] = \"http://www.$LC_APP_NAME.com/assets\"\ndebug = 0\n\n[production : global]\n\n[development : global]\nurls[www] = \"http://www.$LC_APP_NAME.local\"\nurls[static] = \"http://www.$LC_APP_NAME.local/assets\"\ndebug = 1";
createFile($INSTALL_PATH . '/configs/app.ini', $contents);
}
output('creating configs/routes.php', true);
$contents = "<?php\n\$routes = array(\n '/welcome' => array('main', 'welcome')\n);";
createFile($INSTALL_PATH . '/configs/routes.php', $contents);
output('creating controllers/main.php');
$contents = "<?php\nnamespace Controllers;\nuse \Sonic\App;\n\nclass Main extends \Sonic\Controller\n{\n public function index()\n {\n \$this->disableLayout();\n }\n\n public function welcome()\n {\n }\n\n public function error()\n {\n // show error for exception at the highest level\n // if the exception is within a subview we should not show it\n \$this->view->show_error = \$this->request()->getParam('top_level_exception');\n \$this->view->is_dev = App::isDev();\n\n \$this->view->exception = \$this->request()->getParam('exception');\n }\n}";
createFile($INSTALL_PATH . '/controllers/main.php', $contents);
$lib_created = false;
if ($SYMLINK) {
output('creating symlink to libs/Sonic');
symlink($_SERVER['PWD'] . '/lib/Sonic', $INSTALL_PATH . '/libs/Sonic');
$lib_created = true;
}
if (!$lib_created && $ALL_FILES) {
output('copying sonic library to libs/Sonic');
Util::copy('lib/Sonic', $INSTALL_PATH . '/libs/Sonic');
if (!$UNIT_TEST_FRAMEWORK && is_dir($INSTALL_PATH . '/libs/Sonic/UnitTest')) {
Util::removeDir(realpath($INSTALL_PATH . '/libs/Sonic/UnitTest'));
}
$lib_created = true;
}
if (!$lib_created) {
output('copying sonic core to libs/Sonic/Core.php');
createDirectory($INSTALL_PATH . '/libs/Sonic', $merge);
copy('lib/Sonic/Core.php', $INSTALL_PATH . '/libs/Sonic/Core.php');
}
output('creating public_html/.htaccess');
$contents = "# it is recommended to set the environment in your vhost instead of .htaccess\nSetEnv ENVIRONMENT development\nRewriteEngine On\nRewriteRule ^.htaccess$ - [F,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule \.*\$ /index.php [NC,L]";
createFile($INSTALL_PATH . '/public_html/.htaccess', $contents);
output('creating public_html/index.php');
$contents = "<?php\n// make sure to set the timezone if it is not already set in your php.ini\n// date_default_timezone_set('UTC');\n\n\$base_path = str_replace(DIRECTORY_SEPARATOR . 'public_html', '', __DIR__);\nrequire \$base_path . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR . 'Sonic' . DIRECTORY_SEPARATOR . 'Core.php';\n\$app = Sonic\App::getInstance();\n\$app->setBasePath(\$base_path);\n\n// if you would like to use an app delegate uncomment this line\n// \$app->setDelegate('$APP_NAME\App\Delegate');\n\n\$app->start();";
if (!$ALL_FILES) {
$contents = "<?php\nset_include_path(str_replace('/public_html', '/libs', \$_SERVER['DOCUMENT_ROOT']));\ninclude 'Sonic/Core.php';\nuse \Sonic\App;\n\$app = App::getInstance();\n\ntry {\n \$app->start();\n} catch (\\Exception \$e) {\n \$app->handleException(\$e);\n}";
}
createFile($INSTALL_PATH . '/public_html/index.php', $contents);
if ($ALL_FILES) {
output('creating libs/' . $APP_NAME . '/App/Delegate.php', true);
createDirectory($INSTALL_PATH . '/libs/' . $APP_NAME, $merge, true);
createDirectory($INSTALL_PATH . '/libs/' . $APP_NAME . '/App', $merge, true);
$contents = "<?php\nnamespace $APP_NAME\App;\nuse Sonic\App, Sonic\Controller, Sonic\View, Sonic\Layout;\n\n/**\n * for other methods you can implement see Sonic/App/Delegate.php\n */\nclass Delegate extends \Sonic\App\Delegate\n{\n /**\n * called when the application first starts loading before any files are included\n *\n * all application settings should be set here\n *\n * @param string \$mode startup mode of application\n * @return void\n */\n public function appStartedLoading(\$mode)\n {\n // \$this->_app->addSetting(App::AUTOLOAD, true);\n }\n\n /**\n * called when all the core files are done being included for the request\n *\n * all extra files and extensions you need to include for every request should happen here\n *\n * @return void\n */\n public function appFinishedLoading()\n {\n \$this->_app->getRequest()->setHeader('X-Powered-By', 'Sonic Framework ' . App::VERSION);\n // \$this->_app->includeFile('$APP_NAME/Controller.php');\n // \$this->_app->loadExtension('Curl');\n }\n}";
createFile($INSTALL_PATH . '/libs/' . $APP_NAME . '/App/Delegate.php', $contents);
}
output('creating empty favicon file', true);
createFile($INSTALL_PATH . '/public_html/favicon.ico', null);
output('copying turbo.min.js to public_html/assets/js', true);
createDirectory($INSTALL_PATH . '/public_html/assets', $merge, true);
createDirectory($INSTALL_PATH . '/public_html/assets/js', $merge, true);
copy('js/turbo.min.js', $INSTALL_PATH . '/public_html/assets/js/turbo.min.js');
if ($ALL_FILES) {
output('copying extension utility to util/extension', true);
copy('util/extension', $INSTALL_PATH . '/util/extension');
chmod($INSTALL_PATH . '/util/extension', 0755);
}
if ($WITH_JQUERY) {
output('figuring out latest version of jquery...');
$html = file_get_contents('http://jquery.com');
$dom = new DOMDocument();
$dom->loadHtml($html);
$input = $dom->getElementById('jq-production');
$jquery_url = $input->getAttribute('value');
$bits = explode('/', $jquery_url);
$jquery_file_name = array_pop($bits);
output('downloading ' . $jquery_file_name);
$jquery = file_get_contents($jquery_url);
output('copying ' . $jquery_file_name . ' to public_html/assets/js', true);
createFile($INSTALL_PATH . '/public_html/assets/js/' . $jquery_file_name, $jquery);
}
output('creating main layout');
$jquery = $WITH_JQUERY ? " <script src=\"/assets/js/{$jquery_file_name}\"></script>\n": '';
$contents = "<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <title><?php echo \$this->setTitlePattern('$APP_NAME - \${title}'); ?></title>\n</head>\n<body>\n <?php echo \$this->topView()->getHtml(); ?>\n{$jquery}</body>\n</html>";
createFile($INSTALL_PATH . '/views/layouts/main.phtml', $contents);
output('creating index view');
$contents = "<h1>Hello World</h1>\n<a href=\"/welcome\">click here to checkout the welcome page!</a>";
createFile($INSTALL_PATH .'/views/main/index.phtml', $contents);
output('creating welcome view');
$contents = "<?php \$this->title('Welcome!'); ?>\n<h1>Welcome to $APP_NAME</h1>";
createFile($INSTALL_PATH . '/views/main/welcome.phtml', $contents);
output('creating error view');
$contents = "<?php if (\$this->show_error): ?>\n <h1>This is the error view</h1>\n<?php endif; ?>\n\n<?php if (\$this->is_dev && \$this->show_error): ?>\n <h3>debug information</h3>\n <p><?php echo \$this->exception->getMessage(); ?></p>\n <pre><?php echo \$this->exception->getTraceAsString(); ?></pre>\n<?php endif; ?>";
createFile($INSTALL_PATH . '/views/main/error.phtml', $contents);
$ABSOLUTE_PATH = realpath($INSTALL_PATH);
output('creating README');
$contents = "your application has been successfully installed.\n\nnow all you have to do is add this to your vhosts:\n\n<VirtualHost *:80>\n # SetEnv ENVIRONMENT development\n ServerName $LC_APP_NAME.local\n ServerAlias www.$LC_APP_NAME.local\n DocumentRoot \"$ABSOLUTE_PATH/public_html\"\n DirectoryIndex index.php\n</VirtualHost>\n\n<Directory \"$ABSOLUTE_PATH/public_html\">\n Options -Indexes FollowSymLinks\n AllowOverride FileInfo\n Order allow,deny\n Allow from all\n</Directory>\n\nthen add this to your /etc/hosts\n\n127.0.0.1 $LC_APP_NAME.local\n127.0.0.1 www.$LC_APP_NAME.local";
createFile($INSTALL_PATH . '/README', $contents);
output('done');
exit(0);