PageRenderTime 43ms CodeModel.GetById 19ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/build/functions.php

http://zoop.googlecode.com/
PHP | 208 lines | 138 code | 28 blank | 42 comment | 16 complexity | 51bf1636023020f6b3c131645976872d MD5 | raw file
  1<?php
  2
  3define('_n', "\n");
  4
  5function _en()
  6{
  7	echo _n;
  8}
  9
 10function _mkdir($path, $mode = 0775)
 11{
 12	if(is_dir($path))
 13	{
 14		echo "notice: '$path' already exists\n";
 15		return;
 16	}
 17	
 18	_status("creating directory '$path'");
 19	mkdir($path, $mode, true);
 20}
 21
 22function _chgrp($path, $group, $recursive = false)
 23{
 24	$r = $recursive ? 'recursively' : '';
 25	_status("changing group of '$path' to '$group' $r");
 26	if($recursive)
 27		_chgrp_r($path, $group);
 28	else
 29		chgrp($path, $group);
 30}
 31
 32function _chgrp_r($path, $group)
 33{
 34	chgrp($path, $group);
 35	if(!is_dir($path))
 36		return;
 37	
 38	$dir = new DirectoryIterator($path);
 39	foreach($dir as $fileinfo)
 40	    if(!$fileinfo->isDot())
 41			_chgrp_r($path . '/' . $fileinfo->getFilename(), $group);
 42}
 43
 44function _chmod($path, $mode, $recursive = false)
 45{
 46	$r = $recursive ? 'recursively' : '';
 47	$m = decoct($mode);
 48	_status("setting mode of '$path' to '$m' $r");
 49	if($recursive)
 50		_chmod_r($path, $mode);
 51	else
 52		chmod($path, $mode);
 53}
 54
 55function _chmod_r($path, $mode)
 56{
 57	chmod($path, $mode);
 58	if(!is_dir($path))
 59		return;
 60
 61	$dir = new DirectoryIterator($path);
 62	foreach($dir as $fileinfo)
 63	    if(!$fileinfo->isDot())
 64			_chmod_r($path . '/' . $fileinfo->getFilename(), $mode);
 65}
 66
 67/*
 68function _chmod($path, $mode, $recursive = false)
 69{
 70	$m = decoct($mode);
 71	
 72	if($recursive)
 73	{
 74		_status("setting mode of '$path' to '$m' recursively");
 75		_chmod_r($path, $mode);
 76	}
 77	else
 78	{
 79		echo "path = $path\n";
 80		$curmodString = substr(decoct(fileperms ($path)), -3, 3);
 81		var_dump($curmodString);
 82		$newmodString = substr(decoct($mode), -3, 3);
 83		if($curmodString != $newmodString)
 84		{
 85			_status("mode of '$path' is already '$m'");
 86		}
 87		else
 88		{
 89			_status("setting mode of '$path' to '$m'");
 90			chmod($path, $mode);
 91		}
 92	}
 93}
 94
 95function _chmod_r($path, $mode)
 96{
 97	chmod($path, $mode);
 98	if(!is_dir($path))
 99		return;
100
101	$dir = new DirectoryIterator($path);
102	foreach($dir as $fileinfo)
103		if(!$fileinfo->isDot())
104			_chmod_r($path . '/' . $fileinfo->getFilename(), $mode);
105}
106*/
107
108//  this is a hack, we need to just set this up with proper OOP and conveninece functions
109function _forcegen()
110{
111    global $FORCEGEN;
112    $FORCEGEN = true;
113}
114
115function _fetch($path, $params = array())
116{
117	global $_assigns;
118	
119	$templatePath = $path;
120	
121	$gui = new gui();
122	if($_assigns)
123		foreach($_assigns as $name => $value)
124			$gui->assign($name, $value);
125	
126	foreach($params as $name => $value)
127	{
128		// echo "param: $name => $value\n";
129		$gui->assign($name, $value);
130	}
131	
132	return $gui->fetch($templatePath . '.tpl');
133}
134
135function _gen($path, $filePath = '', $params = array())
136{
137	global $FORCEGEN;
138	if(!$filePath)
139		$filePath = $path;
140	$content = _fetch($path, $params);
141	_status("creating generated file '" . getcwd() . '/' . $filePath . "'");
142	
143	if(isset($FORCEGEN) && $FORCEGEN)
144	    $forcegen = true;
145	else
146	    $forcegen = false;
147	
148	if(file_exists($filePath) && !$forcegen)
149		_status("There is already a file at $filePath");
150	
151	file_put_contents($filePath, $content);
152}
153
154function _cd($path)
155{
156	_status("changing directory to '$path'");
157	chdir($path);
158}
159
160function _ln($target, $link)
161{
162	global $FORCEGEN;
163	_status("trying to link '$link' to '$target'");
164	
165	if(file_exists($link))
166	{
167		if($FORCEGEN)
168		{
169			if(is_link($link))
170			{
171				_status("Removing existing symlink at at $link");
172				unlink($link);
173			}
174			else
175			{
176				_status("There is already a non-symlink file at $link");
177				return;
178			}
179		}
180		else
181		{
182			_status("There is already a file at $link");
183			return;
184		}
185	}
186	
187	_status("linking '$link' to '$target'");
188	symlink($target, $link);
189}
190
191function _status($message)
192{
193	echo "status: $message" . _n;
194}
195
196function _assign($name, $value)
197{
198	global $_assigns;
199	_status("assigning '$value' to '$name'");
200	return $_assigns[$name] = $value;
201}
202
203function _run($command, $params)
204{
205	Ex::echoOn();
206	return Ex::pass($command, $params);
207	Ex::echoOff();
208}