Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/*
3
 * Installation Shell Script for CakePHP Console to load Twitter Bootstrap Files
4
 *
5
 * Copyright 2012, visionred (http://visionred.org)
6
 *
7
 * Licensed under The MIT License
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @author 		  Florian Nitschmann (f.nitschmann@visionred.org) 
11
 * @copyright     Copyright 2012, visionred (http://visionred.org)
12
 * @link          http://visionred.org, visionred Web Solutions
13
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
14
 */
15
 
16
App::uses('Folder', 'Utility');
17
 
18
class InstallShell extends AppShell {
19
 
20
	//App folder paths: 
21
	private $app_css_folder;
22
	private $app_img_folder;
23
	private $app_js_folder; 
24
	//Plugin folder paths: 
25
	private $plugin_css_folder;
26
	private $plugin_img_folder; 
27
	private $plugin_js_folder;
28
 
29
	//---Shell functions: 
30
 
31
	/*
32
	 * initialize() - Init shell 
33
	 *
34
	 * @access public
35
	 * @return void 	
36
	 */
37
	public function initialize() {
38
		//Color Setup:
39
		$this->stdout->styles('green', array('text' => 'green'));
40
		$this->stdout->styles('red', array('text' => 'red'));
41
		$this->stdout->styles('yellow', array('text' => 'yellow'));
42
		//Setup plugin folder paths: 
43
		$this->setupFolders(); 
44
	}
45
 
46
 
47
	/*
48
	 * main() - Main Shell Function 
49
	 *
50
	 * @access public
51
	 * @return bool
52
	 */ 	
53
	public function main() {
54
		$this->out("<green>Welcome to Twitter Bootstrap 2.0 for CakePHP 2.x!</green>\n");
55
		$this->out("<yellow>Checking if Bootstrap is already installed:</yellow>\n");
56
		//Check if Bootstrap is already installed: 
57
		$install_status = $this->checkInstallationStatus();
58
		if($install_status == true) {
59
			$this->out(__d('cake_console', '<green>Twitter Bootstrap files are already installed!</green>'));
60
			$setup_task = strtoupper($this->in(__d('cake_console', 'Do you like to set them up again?'), array('Y', 'N'), 'N'));
61
			switch ($setup_task) {
62
				case 'Y':
63
					$this->install(true); 
64
					break;
65
				case 'N':
66
					$this->out(__d('cake_console', '<red>Aborted</red>'));
67
					break;
68
			}
69
		} 
70
		else {
71
			$this->out(__d('cake_console', '<red>Twitter Bootstrap is not installed yet!</red>'));
72
			$this->out("\n");
73
			$setup_task = strtoupper($this->in(__d('cake_console', 'Do you like to set up Twitter Bootstrap?'), array('Y', 'N'), 'Y'));
74
			switch ($setup_task) {
75
				case 'Y':
76
					$this->install(); 
77
					break;
78
				case 'N':
79
					$this->error(__d('cake_console', 'Installation aborted!')); 
80
					break; 
81
			}
82
		}
83
	}
84
 
85
 
86
	/*
87
	 * install($override = false) - Install function for Twitter Bootstrap files
88
	 *
89
	 * @access public
90
	 * @param bool $override [default: false] 
91
	 * @return bool 
92
	 */	
93
	public function install($override = false) {
94
		//Delete existing folders:
95
		if($override == true) {
96
			$this->app_css_folder->delete();
97
			$this->app_img_folder->delete();
98
			$this->app_js_folder->delete(); 
99
		}
100
		//Outputs: 
101
		$this->hr(); 
102
		$this->out(__d('cake_console', '<yellow>Copying new files:</yellow>')); 
103
		$this->out("\n"); 
104
		//CSS:
105
		$this->plugin_css_folder->copy(array(
106
			'to' => $this->app_css_folder->pwd(),
107
			'mode' => 0755,
108
		));
109
		$this->out($this->plugin_css_folder->messages());
110
		if($this->app_css_folder->dirsize() > 0) $this->success();
111
		else $this->failure();
112
		//IMG:  
113
		$this->plugin_img_folder->copy(array(
114
			'to' => $this->app_img_folder->pwd(),
115
			'mode' => 0755
116
		));
117
		$this->out($this->plugin_img_folder->messages());
118
		if($this->app_img_folder->dirsize() > 0) $this->success();
119
		else $this->failure(); 
120
		//JS: 
121
		$this->plugin_js_folder->copy(array(
122
			'to' => $this->app_js_folder->pwd(),
123
			'mode' => 0755
124
		));
125
		$this->out($this->plugin_js_folder->messages());
126
		if($this->app_js_folder->dirsize() > 0) $this->success();
127
		else $this->failure(); 
128
		//Finish installation:
129
		$this->hr();
130
		$this->out(__d('cake_console', '<green>Installation complete!</green>')); 
131
	}
132
 
133
	//---Private Shell functions:
134
 
135
	/*
136
	 * checkInstallStatus() - Check if Twitter Bootstrap files are already located correctly
137
	 *
138
	 * @access public 
139
	 * @return bool 
140
	 */ 
141
	public function checkInstallationStatus() {
142
		//Check the dirsizes:
143
		if($this->app_css_folder->dirsize() > 0 && $this->app_js_folder->dirsize() > 0 && $this->app_img_folder->dirsize() > 0) {
144
			$css_files = $this->app_css_folder->find('bootstrap.*\.css');
145
			$img_files = $this->app_img_folder->find('.*\.png');
146
			$js_files = $this->app_js_folder->find('bootstrap.*\.js');
147
			if(count($css_files) > 0 && count($img_files) > 0 && count($js_files) > 0) return true; 
148
			else return false; 
149
		}
150
		else return false; 
151
	}
152
 
153
 
154
	/*
155
	 * setupFolders() - Create the folder paths to the framework files 
156
	 *
157
	 * @access private
158
	 * @return null 
159
	 */
160
	private function setupFolders() {
161
		//App: 
162
		$this->app_css_folder = new Folder(''.CSS.'twitter'.DS.'bootstrap', true, 0755);
163
		$this->app_img_folder = new Folder(''.IMAGES.'twitter'.DS.'bootstrap', true, 0755);
164
		$this->app_js_folder = new Folder(''.JS.'twitter'.DS.'bootstrap', true, 0755);
165
		//Plugin: 
166
		$this->plugin_css_folder = new Folder(''.APP::pluginPath('BootstrapCake').'webroot'.DS.'css'.DS.'twitter'.DS.'bootstrap');
167
		$this->plugin_js_folder = new Folder(''.APP::pluginPath('BootstrapCake').'webroot'.DS.'js'.DS.'twitter'.DS.'bootstrap');
168
		$this->plugin_img_folder = new Folder(''.APP::pluginPath('BootstrapCake').'webroot'.DS.'img'.DS.'twitter'.DS.'bootstrap');
169
	}
170
 
171
 
172
	/*
173
	 * success() - Print out a success message
174
	 *
175
	 * @access private
176
	 * @return String
177
	 */
178
	private function success() {
179
		$this->out(__d('cake_console', '<green>SUCCESS!</green>'));
180
	}
181
 
182
 
183
	/*
184
	 * failure() - Print out a failure message and quit the cake console session
185
	 *
186
	 * @access private
187
	 * @return String
188
	 */
189
	private function failure() {
190
		$this->error(__d('cake_console', 'Installation failed!')); 
191
	}
192
 
193
}