Skip to content
Browse files

Added a rudimentary file system abstraction class.

Added a class to help manage parameters passed when using PHP from the command line
  • Loading branch information...
1 parent 58c6188 commit 39b0d28ea57112662ce13ef7312d1a6849d7c23a @blobaugh committed Sep 26, 2011
Showing with 366 additions and 0 deletions.
  1. +187 −0 FileSystem.class.php
  2. +179 −0 Params.class.php
View
187 FileSystem.class.php
@@ -0,0 +1,187 @@
+<?php
+
+/**
+ * Simple object to provide a easy consistent interface for managing the file system
+ *
+ * @author Ben Lobaugh <ben@lobaugh.net>
+ * @url http://ben.lobaugh.net
+ * @license Free to use and modify as you choose. Please give credits.
+ */
+class FileSystem {
+
+
+ /**
+ * Moves files inside the $src folder to the $dest folder. If $dest does
+ * not exist it will be created.
+ *
+ * NOTE: Moves the files _inside_ the $src folder, not the $src folder itself
+ *
+ * This function removes $src
+ *
+ * @param String $src - File path to source folder
+ * @param type $dest - File path to destination folder
+ * @return false on failure
+ */
+ function move($src, $dest){
+
+ // If source is not a directory stop processing
+ if(!is_dir($src)) {
+ rename($src, $dest);
+ return true;
+ }
+
+ // If the destination directory does not exist create it
+ if(!is_dir($dest)) {
+ if(!mkdir($dest)) {
+ // If the destination directory could not be created stop processing
+ return false;
+ }
+ }
+
+ // Open the source directory to read in files
+ $i = new DirectoryIterator($src);
+ foreach($i as $f) {
+ if($f->isFile()) {
+ rename($f->getRealPath(), "$dest/" . $f->getFilename());
+ } else if(!$f->isDot() && $f->isDir()) {
+ $this->move($f->getRealPath(), "$dest/$f");
+ @unlink($f->getRealPath());
+ }
+ }
+ @unlink($src);
+ }
+
+
+ /**
+ * Recursively copy files from one directory to another
+ *
+ * @param String $src - Source of files being moved
+ * @param String $dest - Destination of files being moved
+ */
+ function copy($src, $dest){
+
+ // If source is not a directory stop processing
+ if(!is_dir($src)) return false;
+
+ // If the destination directory does not exist create it
+ if(!is_dir($dest)) {
+ if(!mkdir($dest)) {
+ // If the destination directory could not be created stop processing
+ return false;
+ }
+ }
+
+ // Open the source directory to read in files
+ $i = new DirectoryIterator($src);
+ foreach($i as $f) {
+ if($f->isFile()) {
+ copy($f->getRealPath(), "$dest/" . $f->getFilename());
+ } else if(!$f->isDot() && $f->isDir()) {
+ $this->copy($f->getRealPath(), "$dest/$f");
+ }
+ }
+ }
+
+ /**
+ * Creates a new directory. If the path to the directory does not
+ * exist it will also be created
+ *
+ * @param String $path
+ */
+ function mkdir($path) {
+ $path = str_replace("\\", "/", $path);
+ $path = explode("/", $path);
+
+ $rebuild = '';
+ foreach($path AS $p) {
+
+ // Check for Windows drive letter
+ if(strstr($p, ":") != false) {
+ $rebuild = $p;
+ continue;
+ }
+ $rebuild .= "/$p";
+ //echo "Checking: $rebuild\n";
+ if(!is_dir($rebuild)) mkdir($rebuild);
+ }
+ }
+
+ public function findByExtension($path, $ext){
+ $arr = array();
+ $files = $this->ls($path);
+
+ foreach ($files as $f) {
+ $info = pathinfo($path . "/$f");
+ if(isset($info['extension']) && $info['extension'] == $ext)
+ $arr[] = $path . "/$f";
+ }
+ return $arr;
+ }
+
+ /**
+ * List the files in a given directory
+ *
+ * @param String $path
+ * @return Array
+ */
+ public function ls($path) {
+ $arr = array();
+ if(is_dir($path)) {
+ // Open the source directory to read in files
+ $i = new DirectoryIterator($path);
+ foreach($i as $f) {
+ if(!$f->isDot())
+ $arr[] = $f->getFilename();
+ }
+ return $arr;
+ }
+ return false;
+ }
+
+ /**
+ * Determine if a file or folder exists
+ *
+ * @param String $path
+ * @return Boolean
+ */
+ public function exists($path) {
+ if(is_dir($path)) {
+ return true;
+ } else if (file_exists($path)) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Removes a given directory and everything in it
+ *
+ * @param String $path
+ */
+ public function rmdir($path) {
+ // Open the source directory to read in files
+ $i = new DirectoryIterator($path);
+ foreach($i as $f) {
+ if($f->isFile()) {
+ unlink($f->getRealPath());
+ } else if(!$f->isDot() && $f->isDir()) {
+ $this->rmdir($f->getRealPath());
+ //rmdir($f->getRealPath());
+ }
+ }
+ rmdir($path);
+ }
+
+ /**
+ * Remove files or folders
+ *
+ * @param String $path
+ */
+ public function rm($path) {
+ if(is_dir($path)) {
+ $this->rmdir($path);
+ } else {
+ unlink($path);
+ }
+ }
+} // end class
View
179 Params.class.php
@@ -0,0 +1,179 @@
+<?php
+
+/**
+ * Reads parameters passed in from the command line and verifies they all exist.
+ * Provides a useful object to manipulate cmd parameters
+ *
+ * @author Ben Lobaugh <ben@lobaugh.net>
+ * @url http://ben.lobaugh.net
+ * @license Free to use and modify as you choose. Please give credits.
+ */
+class Params {
+
+ /**
+ * Holds a list of parameters for the command line tools
+ *
+ * array (<param_name>, array(<required|optional>, <value>, <message>, <get_from>))
+ * @var Array
+ */
+ private $mParams;
+
+ /**
+ * Holds a list of missing required parameters
+ *
+ * @var String
+ */
+ private $mError;
+
+ public function __construct(){
+ $this->buildmParams();
+ }
+
+ /**
+ * Returns the number of parameters passed
+ *
+ * @return Integer
+ */
+ public function count() {
+ global $argv;
+ return count($argv);
+ }
+
+ /**
+ * Adds a new parameter to the list of parameters that should be available
+ *
+ * @param String $name - The flag to be used on the parameter
+ * @param Boolean $required - Boolean indicating if the parameter is required to be on the command line
+ * @param String $default_value - Default value for optional parameters
+ * @param String $message - Helpful message to be displayed to a user describing the parameter
+ */
+ public function add($name, $required, $default_value, $message) {
+ $this->mParams[$name] = array('required' => $required, 'default_value' => $default_value, 'message' => $message);
+ }
+
+ /**
+ * Removes a parameter from the list of available parameters
+ *
+ * @param String $name
+ */
+ public function remove($name) {
+ unset($this->mParams[$name]);
+ }
+
+ /**
+ * Checks the passed in parameters against the required list to ensure
+ * all parameters that are required are present
+ */
+ public function verify() {
+ $params = $this->getCmdParams();
+
+ $keys = array_keys($params);
+ $msg = "";
+ if(is_array($this->mParams)) {
+ foreach($this->mParams AS $k => $v) {
+ if(isset($params[$k])) $this->set($k, $params[$k]); // Set all values from the cmd line params
+ if($v['required'] /* required */ && (!in_array($k, $keys) || is_null($params[$k]))) {
+ $msg .= "\n{$k} - {$v[2]}";
+ }
+ }
+ if($msg != '') {
+ $this->mError = $msg;
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Builds up $this->mParams with the parameters that currently exist
+ */
+ private function buildmParams() {
+ $params = $this->getCmdParams();
+
+ foreach($params as $k => $v) {
+ $this->add($k, false, 'unknown', 'Auto-added from command line parameter');
+ $this->set($k, $v);
+ }
+ }
+
+ /**
+ * Returns the error message from verify()
+ * @return String
+ */
+ public function getError() {
+ return $this->mError;
+ }
+
+ /**
+ * Sets the value of a command line parameter
+ *
+ * @param String $param
+ * @param String $value
+ */
+ public function set($param, $value) {
+ $this->mParams[$param]['value'] = $value;
+ }
+
+ /**
+ * Returns the value of a single command line parameter
+ *
+ * @param String $param
+ * @return String
+ */
+ public function get($param) {
+ if(isset($this->mParams[$param]['value'])) return $this->mParams[$param]['value'];
+ if(!isset($this->mParams[$param])) return false;
+ return $this->mParams[$param]['default_value'];
+ }
+
+ /**
+ * Returns an associative array of the parameters passed via the command
+ * line or set through a default
+ *
+ * @return Array
+ */
+ public function valueArray() {
+ $arr = array();
+ if(is_array($this->mParams)) {
+ foreach($this->mParams AS $k => $v) {
+ $arr[$k] = $v['value'];
+ }
+ }
+ return $arr;
+ }
+
+
+ /**
+ * Returns all the parameters passed by the command line as key/value pairs.
+ * If a flag is used (param with no value) it will be set to true
+ *
+ * @global Array $argv
+ * @return Array
+ */
+ private function getCmdParams() {
+ global $argv;
+
+ $params = array();
+ for($i = 0; $i < count($argv); $i++) {
+ if(substr($argv[$i], 0, 1) == '-') {
+ if($i <= count($argv)-2 && substr($argv[($i + 1)], 0, 1) != '-') {
+ // Next item is flag
+ $value = $argv[$i + 1];
+ } else {
+ $value = "true";
+ }
+ $key = str_replace("-", "", $argv[$i]);
+ $params[$key] = $value;
+ }
+ }
+ return $params;
+ }
+
+ /**
+ * Convert this object into a string
+ */
+ public function __toString() {
+ return print_r($this->mParams, true);
+ }
+
+} // end class

0 comments on commit 39b0d28

Please sign in to comment.
Something went wrong with that request. Please try again.