Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/*
3
 * Layout Shell Script for CakePHP Console to create custom layout files based on Twitter Bootstrap  
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('File', 'Utility'); 
17
App::uses('Folder', 'Utility');
18
 
19
class LayoutShell extends AppShell {
20
 
21
	private $default_name = 'default';
22
 
23
	private $layout_types = array('fixed', 'fluid'); 
24
 
25
	private $layout_files; 
26
 
27
	private $layout_folder;
28
 
29
	/*
30
	 * initialize() - Init shell 
31
	 *
32
	 * @access public
33
	 * @return void 	
34
	 */
35
	public function initialize() {
36
		//Colors:
37
		$this->stdout->styles('green', array('text' => 'green'));
38
		$this->stdout->styles('red', array('text' => 'red'));
39
		$this->stdout->styles('yellow', array('text' => 'yellow'));
40
		//Custom layout files
41
		$this->layout_files = array(
42
			'fixed' => ''.APP::pluginPath('BootstrapCake').'View'.DS.'Layouts'.DS.'fixed.ctp',
43
			'fluid' => ''.APP::pluginPath('BootstrapCake').'View'.DS.'Layouts'.DS.'fluid.ctp',
44
		);
45
		//App layout folder:
46
		$this->layout_folder = ''.APP.'View'.DS.'Layouts'.DS; 
47
	}
48
 
49
 
50
	/*
51
	 * main() - The main function for the LayoutShell 
52
	 *
53
	 * @access public
54
	 * @return mixed 
55
	 */
56
	public function main() {
57
		$this->out(__d('cake_console', '<green>Welcome to Twitter Bootstrap 2.0 for CakePHP 2.x!</green>'));
58
		$this->out("\n");
59
		//Option parsers: 
60
		$name = $this->params['name'];
61
		$layout_type = $this->params['type'];
62
 
63
		if($this->layoutFileExists($name)) {
64
			$this->out(__d('cake_console', '<red>Layout `'.$name.'.ctp` already exists!</red>'));
65
			$override = strtoupper($this->in(__d('cake_console', 'Do you want to override it?'), array('Y', 'N'), 'Y'));
66
 
67
			switch ($override) {
68
				case 'Y':
69
					$this->createLayoutFile($name, $layout_type, true); 					
70
					break;
71
				case 'N':
72
					$this->error(__d('cake_console', 'Aborted!')); 
73
					break;				
74
			}
75
 
76
		}  
77
		else $this->createLayoutFile($name, $layout_type); 
78
	}
79
 
80
	/*
81
	 * createLayoutFile($name, $layout_type, $override = false) - Create a new custom Twitter Bootstrap Layout file with the parameters 
82
	 *
83
	 * @access private
84
	 * @param String $name - The name for the new layout file
85
	 * @param String $layout_type - Type of the layout (fluid or fixed)
86
	 * @param bool $override [default: false] - Override a layout file if it already exists
87
	 * @return void 
88
	 */
89
	private function createLayoutFile($name, $layout_type, $override = false) {
90
		$layout_file = new File(''.$this->layout_folder.''.$name.'.ctp');
91
		$template_file = new File($this->layout_files[''.$layout_type.'']);
92
 
93
		if($this->layoutFileExists($name) && $override == true) $layout_file->delete(); 
94
 
95
		$this->hr();
96
		$this->out(__d('cake_console', '<yellow>Creating layout file `'.$layout_file->pwd().'`:</yellow>'));
97
 
98
		if($layout_file->create()) {
99
			$content = $template_file->read(); 
100
			if($layout_file->write($content)) $this->out(__d('cake_console', '<green>SUCCESS!</green>')); 
101
			else $this->out(__d('cake_console', 'Could not write contents to `'.$layout_file->pwd().'`'));
102
		} 
103
		else $this->error(__d('cake_console', 'Could not create file!'));
104
	}
105
 
106
	/*
107
	 * layoutFileExists($name) - Check if a layout file is already available
108
	 *
109
	 * @access public
110
	 * 
111
	 * @return bool 
112
	 */
113
	public function layoutFileExists($name) {
114
		if(isset($name)) {
115
			$file = new File(''.$this->layout_folder.''.$name.'.ctp');
116
			return $file->exists();  
117
		}
118
		else return false; 
119
	}
120
 
121
	/*
122
	 * getOptionParser() 
123
	 *
124
	 * @access public 
125
	 * @return void 
126
	 */
127
	public function getOptionParser() {
128
		$parser = parent::getOptionParser();
129
		$parser->description(__d('cake_console', 'The shell to create custom Twitter Bootstrap Layouts'
130
		))->addOption('name', array(
131
			'short' => 'n',
132
			'help' => __d('cake_console', 'The name for your new layout file.'),
133
			'default' => 'default'
134
		))->addOption('type', array(
135
			'short' => 't',
136
			'help' => __d('cake_console', 'Layout type (fixed [default] or fluid)'),
137
			'choices' => $this->layout_types,
138
			'default' => 'fixed'
139
		));
140
		return $parser; 
141
	}
142
 
143
}
144