public function config()
 {
     $config = dirname(__FILE__) . '/user.properties';
     if (file_exists($config)) {
         $config = PurProperties::stringToArray(PurFile::read($config));
     } else {
         echo 'First running the test?' . PHP_EOL;
         echo 'A new file user.properties was created in tests directory.' . PHP_EOL;
         echo 'Modify it to feet your HBase installation, then the tests again.' . PHP_EOL;
         exit;
     }
     return $config;
 }
示例#2
0
	/**
	 * Load a PHP file from a file path or a directory path.
	 * 
	 * Internally, it uses the "PurFile::browse" method to find all
	 * files with a ".php" extension. It accept the same options as
	 * the "PurFile::browse" method.
	 * 
	 * @return 
	 * @param object $path
	 * @param object $options[optional]
	 */
	public static function load($path,$options=array()){
		if(!is_readable($path)) throw new Exception('Provided path not readable: '.$path);
		if(is_file($path)){
			require_once($path);
		}else if(is_dir($path)){
			$files = PurFile::browse($path,array_merge(array(
				'file_only'=>true,
				'absolute'=>true,
				'include'=>'**/*.php'),$options));
			foreach($files as $file){
				require_once($file);
			}
		}
	}
示例#3
0
	/**
	 * 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;
	}
示例#4
0
	/**
	 * Serialize an array to a file. Unless provided, the serialization
	 * format is derived from the file path extension.
	 * 
	 * Options include:
	 * - format Serialization method (js and json accepted);
	 * - from_encoding Encoding of the provided array
	 * - to_encoding Encoding of the destination file
	 * 
	 * Javascript is meant to be understood as JSON surrounded by parenthesis, not
	 * as the full Javascript language. This form is usefull when text editors
	 * support the Javascript synthax and not the JSON one.
	 * 
	 * JSON imposes the UTF-8 encoding. When converting to/from it, it is not the 
	 * PHP array which is encoded/decoded but the JSON string. Also, always 
	 * provide the source encoding when reading from a file written in a 
	 * different encoding than UTF-8.
	 * 
	 * @return boolean True on success
	 * @param string $path Path to the destination file
	 * @param array $array Array to serialize
	 * @param array $options[optional]
	 */
	public static function write($path,array $array,array $options=array()){
		$options = self::sanitize($options);
		if(!isset($options['format'])){
			$dot = strrpos(basename($path),'.');
			if($dot===false) throw new Exception('Format Undertermined From Path: "'.basename($path).'"');
			$format = substr(basename($path),$dot+1);
		}
		switch($format){
			case 'json':
				$array = PurJson::encode($array,$options);
				break;
			case 'js':
				$array = PurJson::encode($array,$options);
				$array = '('.$array.')';
				break;
			default:
				throw new Exception('Unsupported Format: "'.$format.'"');
		}
		return PurFile::write($path,$array,$options);
	}
示例#5
0
	/**
	 * 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;
	}
示例#6
0
	/**
	 * 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;
	}
示例#7
0
	/**
	 * Create a multidimentional array from a property formated file.
	 * 
	 * @return array Multidimensional array
	 * @param string $path Path to the property file
	 */
	public static function read($path){
		return PurProperties::stringToArray(PurFile::read($path));
	}
示例#8
0
	/**
	 * Create an image resource base on the provided file path.
	 * 
	 * Options may include:
	 * - *width*: Canvas width of the generated image or resource; "100" and "80+top" (with top defined as 20) for 100 pixels
	 * - *height*: Canvas height of the generated image or resource; "50" and "25+top" (with top defined as 20) for 75 pixels
	 * - *top*: Offset from the top of the source in pixel
	 * - *left*: Offset from the left of the source in pixel
	 * - *bottom*: Offset from the bottom of the source in pixel
	 * - *right*: Offset from the right of the source in pixel
	 * - *temp_dir*: Path to temporary working directory
	 * - *gm_convert*: Path to the gm convert utility (used for psd images, usually "gm convert" or "convert")
	 * 
	 * Create a new transparent resource of 100x75 pixels:
	 * * PurImage::resource(array('width'=>100,'height'=>75));
	 * 
	 * Create a resource from a psd file (with "gm" available in your path as "gm convert...")
	 * * PurImage::resource('path/to/image.psd');
	 * 
	 * Create a resource from a psd file (with "gm" not available but installed)
	 * * PurImage::resource('path/to/image.psd',array('gm_convert'=>'/usr/bin/gm convert'));
	 * 
	 * @return resource Resource associated to the file path
	 * @param mixed $source File path referencing an image or null to create a transparent resource
	 * @param array $options[optional] Configuration array
	 */
	public static function resource($source=null,array $options=array()){
		if(is_array($source)){
			$options = $source;
			$source = null;
		}
		if(empty($source)){
			if(empty($options['width'])||empty($options['height'])){
				throw new InvalidArgumentException('Missing Options: width and height required when creating a new resource');
			}
			$resource = imagecreatetruecolor($options['width'],$options['height']);
			imagesavealpha($resource, true);
			imagefill($resource,0,0,imagecolorallocatealpha($resource,255,255,255,127));
			return $resource;
		}else if(is_string($source)){
			if(!is_readable($source)) throw new InvalidArgumentException('Invalid Source Path: '.PurLang::toString($source).'');
			$type = exif_imagetype($source);
			switch($type){
				case IMAGETYPE_GIF:
					$source = imagecreatefromgif($source);
					break;
				case IMAGETYPE_JPEG:
					$source = imagecreatefromjpeg($source);
					break;
				case IMAGETYPE_PNG:
					$source = imagecreatefrompng($source);
					break;
				case IMAGETYPE_SWF:
					break;
				case IMAGETYPE_PSD:
					if(!isset($options['gm_convert'])){
						throw new Exception('Missing Option For PSD Image Type: gm_convert');
					}
					$temp = (isset($options['temp_dir'])?$options['temp_dir']:sys_get_temp_dir()).'/'.uniqid();
					$cmd = $options['gm_convert'].' -flatten '.escapeshellarg($source).' '.escapeshellarg('png:'.$temp);
					exec($cmd);
					$source = imagecreatefrompng($temp);
					PurFile::delete($temp);
					break;
				case IMAGETYPE_BMP:
					break;
				case IMAGETYPE_TIFF_II:
					break;
				case IMAGETYPE_TIFF_MM:
					break;
				case IMAGETYPE_JPC:
					break;
				case IMAGETYPE_JP2:
					break;
				case IMAGETYPE_JPX:
					break;
				case IMAGETYPE_JB2:
					break;
				case IMAGETYPE_SWC:
					break;
				case IMAGETYPE_IFF:
					break;
				case IMAGETYPE_WBMP:
					$source = imagecreatefromwbmp($source);
					break;
				case IMAGETYPE_XBM:
					$source = imagecreatefromxbm($source);
					break;
			}
			$created = true;
		}else if(is_resource($source)){
			$created = false;
		}else{
			throw new InvalidArgumentException('Invalid Source: empty, string or resource accepted');
		}
//		width
//		height
//		top
//		left
//		bottom
//		right
//		imagecopyresampled($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
		$sourceWidth = imagesx($source);
		$sourceHeight = imagesy($source);
		// Sanitize width and height
		if(!empty($options['width'])&&!empty($options['height'])){
			if($options['width']!=$sourceWidth&&$options['height']!=$sourceHeight){
				$resized = true;
			}
		}else if(!empty($options['width'])&&$options['width']!=$sourceWidth){
			$resized = true;
			$options['height'] = $sourceHeight*$options['width']/$sourceWidth;
		}else if(!empty($options['height'])&&$options['height']!=$sourceHeight){
			$resized = true;
			$options['width'] = $sourceWidth*$options['height']/$sourceHeight;
		}
		// Sanitize resize as an array with width and height keys
		if(!empty($options['resize'])){
			$resized = true;
			switch(gettype($options['resize'])){
				case 'string':
					$options['resize'] = explode('x',$options['resize']);
				case 'array':
					if(empty($options['resize'])||count($options['resize'])>2){
						throw new Exception('Invalid "resize": '.PurLang::toString($options['resize']));
					}
					// Deal with map
					if(!empty($options['resize']['width'])){
						$width = $options['resize']['width'];
					}
					if(!empty($options['resize']['height'])){
						$height = $options['resize']['height'];
					}
					// Deal with index
					if(!isset($width)){
						$width = array_shift($options['resize']);
					}
					if(!isset($height)){
						$height = array_shift($options['resize']);
					}
					// Deal with *
					if($width=='*') $width = null;
					if($height=='*') $height = null;
					// Rebuild resize
					$options['resize']['width'] = $width;
					unset($width);
					$options['resize']['height'] = $height;
					unset($height);
					if(empty($options['resize']['width'])){
						$options['resize']['width'] = $sourceWidth*$options['resize']['height']/$sourceHeight;
					}else if(empty($options['resize']['height'])){
						$options['resize']['height'] = $sourceHeight*$options['resize']['width']/$sourceWidth;
					}
					break;
				default:
					throw new Exception('Invalid "resize": '.PurLang::toString($options['resize']));
			}
		}else{
			$options['resize'] = array('width'=>$sourceWidth,'height'=>$sourceHeight);
		}
		if(isset($resized)){
			$destination = self::resource(null,$options);
			imagecopyresampled($destination,$source,0,0,0,0,
				$options['resize']['width'],$options['resize']['height'],
				$sourceWidth,$sourceHeight);
			if($created){
				imagedestroy($source);
			}
			$source = $destination;
		}
		return $source;
	}