コード例 #1
0
ファイル: PurFile.php プロジェクト: rintaun/himawari
	/**
	 * Write a string to a file. If file does not exist, it will be created. If file exists,
	 * its current content will be overwritten unless the "append" option is provided. Parent
	 * directories will be created if they do not already exist.
	 * 
	 * Exemples:
	 * 
	 * * // Touching a file
	 * * PurFile::write('/path/to/file.txt');
	 * 
	 * * // (Over)Write content to a file
	 * * PurFile::write('/path/to/file.txt','my content');
	 * 
	 * * // Add or write (if new) content to a file
	 * * PurFile::write('/path/to/file.txt','my content',array('append'));
	 * 
	 * Options may include:
	 * - permissions: chmod mask, apply only when the file is created, default to php 0644
	 * - append: boolean If true, new content will be appended to the destination file if it exists
	 *  
	 * @return boolean true
	 * @param mixed(string or resource) $path Target file path
	 * @param string $content Content to write
	 * @param object $options[optional]
	 */
	public static function write($path,$content='',$options=array()){
		$options = PurArray::sanitize($options);
		switch(gettype($path)){
			case 'string':
				if(!file_exists($path)){
					$dir = dirname($path);
					PurFile::mkdir($dir);
					//if(!is_writable($dir)) throw new Exception('File Not Writable in Dir: '.$dir);
					$isNew = true;
				}else if(!is_writable($path)) throw new Exception('File Not Writable: '.$path);
				$resource = fopen($path,empty($options['append'])?'w':'a');
				if(isset($isNew)&&isset($options['permissions'])){
					chmod($path,$options['permissions']);
				}
				break;
			case 'resource':
				$resource = $path;
				break;
			default:
				throw new InvalidArgumentException('Path is expected to be a string or a resource');
		}
		flock($resource,LOCK_EX);
		fwrite($resource,$content);
		flock($resource,LOCK_UN);
		fclose($resource);
		return true;
	}
コード例 #2
0
ファイル: PurGit.php プロジェクト: rintaun/himawari
	/**
	 * Options may include:
	 * -   *bare* Intialize a bare Git repository
	 * -   *bin* Path to the Git command
	 * -   *cwd* Directory or list of directories to initialize
	 * 
	 * @param array $options required Must include the "cwd" setting.
	 * @return true Return true on success, otherwise an exception is thrown
	 * 
	 * @param string $cwd required Path to the directory to initialize
	 * @param array $options optional All parameters are optional
	 * @return true Return true on success, otherwise an exception is thrown
	 */
	public static function init(){
		$args = func_get_args();
		switch(count($args)){
			case 1:
				if(is_string($args[0])){
					$options = array(
						'cwd' => array($args[0]=>true));
				}else if(is_array($args[0])){
					$options = PurArray::sanitize($args[0]);
					if(!isset($options['cwd'])){
						throw new InvalidArgumentException('Options must include the "cwd" key');
					}else if(is_string($options['cwd'])){
						$options['cwd'] = array($options['cwd']=>true);
					}else if(!is_array($options['cwd'])){
						throw new InvalidArgumentException('Invalid "cwd" option: "'.PurLang::toString($options['cwd']).'"');
					}
				}else{
					throw new InvalidArgumentException('Invalid Arguments: "'.PurLang::toString($args).'"');
				}
				break;
			case 2:
				if(is_string($args[0])&&is_array($args[1])){
					$options = PurArray::sanitize($args[1]);
					if(!isset($options[1]['cwd'])){
						$options['cwd'] = array();
					}
					$options['cwd'][$args[0]] = true;
				}else{
					throw new InvalidArgumentException('Invalid Arguments: "'.PurLang::toString($args).'"');
				}
				break;
			default:
				throw new InvalidArgumentException('Invalid Arguments Count: '.PurLang::toString($args));
		}
		unset($args);
		if(empty($options['bin'])){
			$options['bin'] = self::$bin;
		}
		while(list($cwd,) = each($options['cwd'])){
			if(!is_string($cwd)){
				throw new InvalidArgumentException('Invalid "cwd" option: "'.PurLang::toString($cwd).'"');
			}
			$command = $options['bin'];
			$command .= ' init';
			if(!empty($options['bare'])){
				$command .= ' --bare';
			}
			if(!is_dir($cwd)){
				PurFile::mkdir($cwd);
			}
			PurCli::exec($command,array_merge($options,array('cwd'=>$cwd)));
		}
		return true;
	}
コード例 #3
0
ファイル: PurHttp.php プロジェクト: rintaun/himawari
	/**
	 * Return a URL content or write its output to file if a destination is provided.
	 * 
	 * Note, this implementation require the PHP CURL extension installed.
	 * 
	 * @param string $url Content source
	 * @param string $destination [optional] File destination where output should be written
	 * @return mixed URL content or boolean true if a destination is provided
	 */
	public static function read($url,$destination=null){
		if(!function_exists('curl_init')){
			throw new Exception('PHP CURL Extension Required');
		}
		$ch = curl_init($url);
		curl_setopt($ch,CURLOPT_HEADER,false);
		if($destination){
			PurFile::mkdir(dirname($destination));
			if(file_exists($destination)) PurFile::delete($destination);
			$fp = fopen($destination,'w');
			curl_setopt($ch,CURLOPT_FILE,$fp);
		}else{
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,1) ;
		}
		if(($data = curl_exec($ch))===false){
			throw new Exception('Failed to download url: "'.$url.'"');
		}
		$header = curl_getinfo($ch);
		curl_close($ch);
		if($destination){
			fclose($fp);
		}
		if($header['http_code']!=self::SUCCESS_OK){
			if($destination){
				PurFile::delete($destination);
			}
			throw new Exception('Download Failed: "'.constant('PurHTTP::'.'CODE_'.$header['http_code']).'" ('.$header['http_code'].')');
		}
		return $data;
	}