예제 #1
0
파일: PurGit.php 프로젝트: rintaun/himawari
	/**
	 * Return an array representation of the git status command.
	 * 
	 * Options may include:
	 * -   *absolute* Return absolute paths
	 * -   *cwd* Directory from from where the status must be run
	 * -   *bin* Path to the Git command
	 * -   *relative* Relative to the Git project root directory
	 * 
	 * Structure of the returned array:
	 * -   changed_to_be_committed
	 * -   changed_but_not_updated
	 * -   untracked_files
	 * 
	 * @return array Decomposed status
	 * 
	 * @param array $options required Options used to alter the method behavior
	 * @return array Decomposed status
	 * 
	 * @param string $cwd required Path to the directory to initialize
	 * @return array Decomposed status
	 */
	public static function status(){
		$args = func_get_args();
		switch(count($args)){
			case 0:
				$options = array();
				break;
			case 1:
				if(is_string($args[0])){
					$options = array(
						'cwd' => $args[0]);
				}else if(is_array($args[0])){
					$options = $args[0];
				}else{
					throw new InvalidArgumentException('Invalid Arguments: "'.PurLang::toString($args).'"');
				}
				break;
			default:
				throw new InvalidArgumentException('Invalid Arguments Count: '.PurLang::toString($args));
		}
		unset($args);
		$options['return_message'] = 1;
		if(empty($options['bin'])){
			$options['bin'] = self::$bin;
		}
		$command = $options['bin'];
		$command .= ' status';
		$out = PurCli::exec($command,$options);
		$status = array(
			'changed_to_be_committed'=>array(),
			'changed_but_not_updated'=>array(),
			'untracked_files'=>array());
		$type = null;
		foreach($out as $line){
			if($line=='# Changes to be committed:'){
				$type = 'changed_to_be_committed';
			}else if($line=='# Changed but not updated:'){
				$type = 'changed_but_not_updated';
			}else if($line=='# Untracked files:'){
				$type = 'untracked_files';
			}else{
				if(preg_match('/^#\t((new file|modified|deleted): +)?(.*)/',$line,$matches)){
//					if(isset($options['cwd'])){
//						$matches[3] = PurPath::relative($options['cwd'],getcwd().'/'.$matches[3]);
//					}
					if(!empty($options['absolute'])){
						$matches[3] = PurPath::clean($options['cwd'].'/'.$matches[3]);
					}else if(!empty($options['relative'])){
						$matches[3] = PurPath::relative(self::dir($options),$options['cwd'].'/'.$matches[3]);
					}
					if(empty($matches[2])){
						$status[$type][] = $matches[3];
					}else{
						$status[$type][$matches[2]][] = $matches[3];
					}
				}
			} 
		}
		while(list($k,$v) = each($status)){
			if(empty($v)){
				unset($status[$k]);
			}
		}
		reset($status);
		return $status;
	}
예제 #2
0
	/**
	 * Combine two paths together when appropriate.
	 * 
	 * The first path may be empty or absolute, an error will be thrown 
	 * if a relative path is provided.
	 * 
	 * The second path may be relative in which case it will be combined 
	 * with the first path if not empty or absolute in which case it will be
	 * return as is.
	 * 
	 * Note, all path will be cleaned up before being returned.
	 * 
	 * @param object $path1
	 * @param object $path2
	 * @return 
	 */
	public static function combine($path1,$path2){
		if(!empty($path1)&&!self::isAbsolute($path1)){
			throw new InvalidArgumentException('First argument must be empty or an asolute path');
		}
		if(self::isAbsolute($path2)){
			return PurPath::clean($path2);
		}
		return PurPath::clean(empty($path1)?$path2:$path1.'/'.$path2);
	}
예제 #3
0
	/**
	 * Create directories and sub-directories (targeting PHP 5.1). Note,
	 * it also enforces correct permission mask on created directories.
	 * 
	 * @param string $dir Path to the directory to be created
	 * @param int $mode [optional] Permission applied to newly created directories
	 * @return mixed(string,boolean) Path to the newly created directory on success, otherwise boolean false
	 */
	public static function mkdir($dir, $mode=0755){
		$dir = PurPath::clean($dir);
		$return = $dir;
		if(is_dir($dir)) return $dir;
		$stack = array(basename($dir));
		$path = null;
		while ( ($d = dirname($dir) ) ){
			if ( !is_dir($d) ){
				$stack[] = basename($d);
				$dir = $d;
			} else{
				$path = $d;
				break;
			}
		}
	
		if ( ( $path = realpath($path) ) === false ){
			// Return false if path doesn't exist
			throw new Exception('Invalid directory '.$dir);
		}
		
		$created = array();
		for ( $n = count($stack) - 1; $n >= 0; $n-- ){
			$s = $path . '/'. $stack[$n];
			if(!is_writable(dirname($s))) throw new Exception('Permission Denied: '.$s);
			if ( !mkdir($s, $mode) ){
				for ( $m = count($created) - 1; $m >= 0; $m-- )
					rmdir($created[$m]);
				throw new Exception('Failed to create directory :"'.$s.'"');
			}
			if(!chmod($s, $mode)){
				for ( $m = count($created) - 1; $m >= 0; $m-- )
					rmdir($created[$m]);
				throw new Exception('Failed to update directory permission : "'.$mode.'" in "'.$s.'"');
			}
			$created[] = $s;	  
			$path = $s;
		}
		return $return;
	}
예제 #4
0
/** Sanitization of $_ENV['PATH'] into $_GET-like array.
 * Example: /varname:varvalue/ would become Array('varname' => 'varvalue')
 * @global array $_ENV['PATHVARS']
 */
$_ENV['PATHVARS'] = isset($pathvars) ? $pathvars : array();

/** Default template when there is no database or no template entry.
 * @global string $_ENV['TEMPLATE']
 */
$_ENV['TEMPLATE'] = 'kyrie';

/** Himawari's base directory on the filesystem
 * @global string $_ENV['BASE_PATH']
 */
$_ENV['BASE_PATH'] = PurPath::clean(dirname(__FILE__));

/** The current script's directory
 * @global string $_ENV['CURRENT_PATH']
 */
$_ENV['CURRENT_PATH'] = getcwd();

/** The data directory, where the database and uploaded files are stored.
 * @global string $_ENV['DATA_DIR']
 */
$_ENV['DATA_DIR'] = $_ENV['BASE_PATH'] . '/' . $_CONFIG['DATA_DIRNAME'] . '/';

if ((!@is_dir($_ENV['DATA_DIR']) && !mkdir($_ENV['DATA_DIR'])) || !is_writable($_ENV['DATA_DIR']))
	trigger_error("DATA_DIR ({$_ENV['DATA_DIR']}) does not exist or is not writable.", E_USER_ERROR);

/** The URL to the data directory