예제 #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
	/**
	 * Print a readable stacktrace. If no strace is provided, will generate a new one.
	 * 
	 * Options may include:
	 * - prefix: string to insert a the beginning of each lines, default to empty
	 * - suffix: string to insert at the end of each lines, default to windows carriage return '\r\n'
	 * - base: transform absolute path to relative
	 * 
	 * @return 
	 * @param object $trace[optional]
	 * @param object $options[optional]
	 */
	public static function traceAsString($trace=null,$options=array()){
		if(is_null($trace)) $trace = PurLang::trace();
		elseif(!is_array($trace)) throw new InvalidArgumentException('Trace must be an array or null: "'.self::toString($trace).'" provided');
		$content = '';
		foreach($trace as $k=>$v){
			if(isset($v['file'])){
				if(isset($options['prefix'])) $content .= $options['prefix'];
				$file = $v['file'];
				if(!empty($options['base'])){
					$_file = PurPath::relative($options['base'],$file);
					if(strpos($_file,'../')!==0){
						$file = $_file;
					}
					unset($_file);
				}
				$content .= $file.':'.$v['line']." ";
				$content .= (array_key_exists('class',$v)?$v['class']:'').(array_key_exists('type',$v)?$v['type']:'').$v['function'].'()';
				$content .= (isset($options['suffix']))?$options['suffix']:"\r\n";
			}
		}
		return $content;
	}
예제 #3
0
	/**
	 * Extract the extension of a provided path.
	 * 
	 * @return string
	 * @param object $file
	 */
	public static function extension($path){
		if(preg_match('/^https?:\/\//i', $path)){
			if($q=strpos($path,'?')){
				$path = substr($path,0,$q);
			}
			//return substr($path,strrpos($path,'.')+1);
		}
		$path = PurPath::filename($path);
		if(($position=strrpos($path,'.'))!==false){
			return substr($path,$position+1);
		}else{
			return '';
		}
		
	}
예제 #4
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;
	}
예제 #5
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