/inc/path.php
PHP | 160 lines | 99 code | 12 blank | 49 comment | 10 complexity | cd123975aa8fcc769b1dfa75712e36ae MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
- <?php
- /************************************************************************
- * @project Gutuma Newsletter Managment
- * @author Rowan Seymour
- * @copyright This source is distributed under the GPL
- * @file The path class
- */
-
- /**
- * The path class
- */
- class gu_path
- {
- private $path;
-
- /**
- * Constructor
- */
- public function __construct($path)
- {
- $this->path = $path;
- }
-
- /**
- * Converts to a string, only called automatically in PHP 5.2.0+
- */
- public function __toString()
- {
- return $this->path;
- }
-
- /**
- * Checks if this path exists
- * @return TRUE if this path exists, else FALSE
- */
- public function exists()
- {
- return file_exists($this->path);
- }
-
- /**
- * Checks if this path exists and is a directory or a file
- * @return bool TRUE is this path is a directory, else FALSE
- */
- public function is_directory()
- {
- return is_dir($this->path);
- }
-
- /**
- * Checks if this exists and is a file or a directory
- * @return bool TRUE is this path is a file, else FALSE
- */
- public function is_file()
- {
- return is_file($this->path);
- }
-
- /**
- * Gets the extension assuming this path is a file
- * @return string The extension
- */
- public function get_extension()
- {
- $ext = strrchr($this->path, '.');
- return ($ext === FALSE || strlen($ext) <= 1) ? NULL : substr($ext, 1);
- }
-
- /**
- * Gets the filename assuming this path is a file
- * @param $inc_extension TRUE if extension should be included, else FALSE
- * @return string The name component
- */
- public function get_name($inc_extension = TRUE)
- {
- return $inc_extension ? basename($this->path) : basename($this->path, strrchr($this->path, '.'));
- }
-
- /**
- * Gets the path of the parent directory
- * @return gu_path The parent directory path
- */
- public function get_parent()
- {
- return new gu_path(dirname($this->path));
- }
-
- /**
- * Gets the children of this path, i.e. subdirectories and files assuming its a directory
- * @param $inc_hidden TRUE if hidden files should be included, else FALSE
- * @return array The children
- */
- public function get_children($inc_hidden = FALSE)
- {
- $children = array();
-
- if ($dh = @opendir($this->path)) {
- while (($item = readdir($dh)) !== FALSE) {
- if ($item != '.' && $item != '..' && ($inc_hidden || $item[0] != '.'))
- $children[] = new gu_path($this->path.DIRECTORY_SEPARATOR.$item);
- }
- closedir($dh);
- return $children;
- }
- else
- return NULL;
- }
-
- /**
- * Canonizes the path, even if it doesn't exist. From http://uk2.php.net/realpath
- * @return string The canonized path
- */
- public function get_absolute()
- {
- $p = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->path);
- $parts = array_filter(explode(DIRECTORY_SEPARATOR, $p), 'strlen');
- $absolutes = array();
- foreach ($parts as $part) {
- if ('.' == $part)
- continue;
- if ('..' == $part)
- array_pop($absolutes);
- else
- $absolutes[] = $part;
- }
- return implode(DIRECTORY_SEPARATOR, $absolutes);
- }
-
- /**
- * Renames the file or directory pointed to by this path
- * @param $new_path The new path
- * @return TRUE if successful, else FALSE
- */
- public function rename($new_path)
- {
- gu_debug('rename('.$this->path.', '.$new_path.')');
- if (($res = rename($this->path, $new_path)) === TRUE)
- $this->path = $new_path;
- return $res;
- }
-
- /**
- * Moves the file or directory pointed to by this path. If new path is an existing
- * file it will be overwritten, and if its an existing directory, then this file or
- * directory will be moved inside it.
- * @param $new_path The new path
- * @return TRUE if successful, else FALSE
- */
- public function move($new_path)
- {
- if (is_dir($new_path))
- $new_path = $new_path.DIRECTORY_SEPARATOR.$this->get_name();
- elseif (is_file($new_path))
- unlink($new_path);
-
- return $this->rename($new_path);
- }
- }
-
- ?>