parent
9298f862be
commit
dcb5b5c410
@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Déclaration de la classe CommandLine\Argument\Parser\PathParser
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace CommandLine\Argument\Parser;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use UnexpectedValueException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parseur d'un chemin (fichier ou répertoire)
|
||||||
|
*
|
||||||
|
* Renvoie un {@see stdStringClass} avec les attributs suivants :
|
||||||
|
* - *fullpath* : chemin complet
|
||||||
|
* - directory : chemin du répertoire contenant
|
||||||
|
* - filename : nom du fichier (extension inclue)
|
||||||
|
* - basename : nom du fichier, sans l'extension
|
||||||
|
* - extension : extension du fichier
|
||||||
|
*
|
||||||
|
* @package CommandLine\Argument\Parser
|
||||||
|
*/
|
||||||
|
class PathParser implements IValueParser {
|
||||||
|
/**
|
||||||
|
* @var string[] Les expressions régulières pour identifier un chemin
|
||||||
|
*/
|
||||||
|
const REGEX_PATH = array(
|
||||||
|
'unix' => /** @lang PhpRegExp */ '@^(?<directory>/*(?:[^/\n]+(?:/+[^/\n]+)*/+)?)(?<filename>(?<basename>[^/\n]+?)(?:(?:\.(?<extension>[a-zA-Z0-9_]+))|(?:/+))?)$@',
|
||||||
|
'windows' => /** @lang PhpRegExp */ '@^(?<directory>(?:(?:(?:(?:[a-zA-Z]:)|[\\\\/])[\\\\/])|(?![\\\\/]))(?:[^<>:"/\\\\|?*]+(?:[\\\\/][^<>:"/\\\\|?*]+)*[\\\\/]+)?)(?<filename>(?<basename>[^<>:"/\\\\|?*]+?)(?:(?:\.(?<extension>[a-zA-Z0-9_]+))|(?:[\\\\/]))?)$@',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool Est-ce que le fichier doit exister ?
|
||||||
|
*/
|
||||||
|
protected $_mustExist;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crée un nouveau parseur
|
||||||
|
*
|
||||||
|
* @param bool $mustExists Est-ce que le chemin doit exister ?
|
||||||
|
*/
|
||||||
|
public function __construct ($mustExists = false) {
|
||||||
|
$this->setMustExists($mustExists);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Spécifie si le chemin doit exister ou non
|
||||||
|
*
|
||||||
|
* @param bool $mustExists True si le chemin doit exister, sinon False
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see $_mustExists
|
||||||
|
*/
|
||||||
|
public function setMustExists ($mustExists = false) {
|
||||||
|
if (!is_bool($mustExists)) {
|
||||||
|
throw new InvalidArgumentException('Le contrôle si le chemin doit exister ou non n\'est pas booléen : ' . $mustExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_mustExist = $mustExists;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function parseValue ($arg) {
|
||||||
|
foreach (self::REGEX_PATH as $regex) {
|
||||||
|
if (preg_match($regex, $arg, $matches, PREG_UNMATCHED_AS_NULL)) {
|
||||||
|
if ($this->_mustExist && !file_exists($matches[0])) {
|
||||||
|
throw new UnexpectedValueException('Le chemin n\'existe pas : ' . $arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
$out = new stdStringClass('fullpath');
|
||||||
|
$out->fullpath = $matches[0];
|
||||||
|
$out->directory = $matches['directory'] ?? '';
|
||||||
|
$out->filename = $matches['filename'] ?? '';
|
||||||
|
$out->basename = $matches['basename'] ?? '';
|
||||||
|
$out->extension = $matches['extension'] ?? '';
|
||||||
|
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UnexpectedValueException('La valeur n\'est pas un chemin valide : ' . $arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getValueDescription () {
|
||||||
|
return 'path';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Est-ce que le chemin doit exister ?
|
||||||
|
*
|
||||||
|
* @return bool True si le chemin doit exister, sinon False
|
||||||
|
*
|
||||||
|
* @see $_mustExists
|
||||||
|
*/
|
||||||
|
public function getMustExist () {
|
||||||
|
return $this->_mustExist;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Déclaration de la classe CommandLine\Argument\Parser\stdStringClass
|
||||||
|
*/
|
||||||
|
namespace CommandLine\Argument\Parser;
|
||||||
|
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Une stdClass habituelle, mais avec un possibilite d'être traité comme une chaine de caractère
|
||||||
|
*
|
||||||
|
* L'un des attributs de la classe doit être choisi pour être utilisé comme valeur lors de la conversion en chaine de caractères
|
||||||
|
*/
|
||||||
|
class stdStringClass extends stdClass {
|
||||||
|
/**
|
||||||
|
* @var string Le nom de la propriété
|
||||||
|
*/
|
||||||
|
private $_property;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crée une nouvelle instance
|
||||||
|
*
|
||||||
|
* @param string $property Le nom de la propriété
|
||||||
|
*/
|
||||||
|
public function __construct ($property) {
|
||||||
|
$this->_property = $property;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converti l'instance en chaine de caractère
|
||||||
|
*
|
||||||
|
* @return string La valeur de l'attribut choisi (convertion forcé en chaine de caractères)
|
||||||
|
*/
|
||||||
|
public function __toString () {
|
||||||
|
return (string)$this->{$this->_property};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue