Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
Hoa\Console\Readline\Autocompleter\Path Class Reference
Inheritance diagram for Hoa\Console\Readline\Autocompleter\Path:

Public Member Functions

 __construct ($root=null,\Closure $iteratorFactory=null)
 
 complete (&$prefix)
 
 getWordDefinition ()
 
 setRoot ($root)
 
 getRoot ()
 
 setIteratorFactory (\Closure $iteratorFactory)
 
 getIteratorFactory ()
 

Static Public Member Functions

static getDefaultIteratorFactory ()
 

Public Attributes

const PWD = null
 

Protected Attributes

 $_root = null
 
 $_iteratorFactory = null
 

Detailed Description

Class .

Path autocompleter.

Definition at line 47 of file Path.php.

Constructor & Destructor Documentation

Hoa\Console\Readline\Autocompleter\Path::__construct (   $root = null,
\Closure  $iteratorFactory = null 
)

Constructor.

Parameters
string$rootRoot.
\Closure$iteratorFactoryIterator factory (please, see the self::setIteratorFactory method).
Returns
void

Definition at line 81 of file Path.php.

84  {
85  if (null === $root) {
86  $root = static::PWD;
87  }
88 
89  $this->setRoot($root);
90 
91  if (null !== $iteratorFactory) {
92  $this->setIteratorFactory($iteratorFactory);
93  }
94 
95  return;
96  }
setIteratorFactory(\Closure $iteratorFactory)
Definition: Path.php:200

Here is the call graph for this function:

Member Function Documentation

Hoa\Console\Readline\Autocompleter\Path::complete ( $prefix)

Complete a word. Returns null for no word, a full-word or an array of full-words.

Parameters
string&$prefixPrefix to autocomplete.
Returns
mixed

Implements Hoa\Console\Readline\Autocompleter\Autocompleter.

Definition at line 105 of file Path.php.

106  {
107  $root = $this->getRoot();
108 
109  if (static::PWD === $root) {
110  $root = getcwd();
111  }
112 
113  $path = $root . DS . $prefix;
114 
115  if (!is_dir($path)) {
116  $path = dirname($path) . DS;
117  $prefix = basename($prefix);
118  } else {
119  $prefix = null;
120  }
121 
122  $iteratorFactory = $this->getIteratorFactory() ?:
123  static::getDefaultIteratorFactory();
124 
125  try {
126  $iterator = $iteratorFactory($path);
127  $out = [];
128  $length = mb_strlen($prefix);
129 
130  foreach ($iterator as $fileinfo) {
131  $filename = $fileinfo->getFilename();
132 
133  if (null === $prefix ||
134  (mb_substr($filename, 0, $length) === $prefix)) {
135  if ($fileinfo->isDir()) {
136  $out[] = $filename . '/';
137  } else {
138  $out[] = $filename;
139  }
140  }
141  }
142  } catch (\Exception $e) {
143  return null;
144  }
145 
146  $count = count($out);
147 
148  if (1 === $count) {
149  return $out[0];
150  }
151 
152  if (0 === $count) {
153  return null;
154  }
155 
156  return $out;
157  }

Here is the call graph for this function:

static Hoa\Console\Readline\Autocompleter\Path::getDefaultIteratorFactory ( )
static

Get default iterator factory (based on ).

Returns

Definition at line 223 of file Path.php.

224  {
225  return function ($path) {
226  return new \DirectoryIterator($path);
227  };
228  }
Hoa\Console\Readline\Autocompleter\Path::getIteratorFactory ( )

Get iterator factory.

Returns

Definition at line 213 of file Path.php.

214  {
216  }

Here is the caller graph for this function:

Hoa\Console\Readline\Autocompleter\Path::getRoot ( )

Get root.

Returns
string

Definition at line 188 of file Path.php.

189  {
190  return $this->_root;
191  }

Here is the caller graph for this function:

Hoa\Console\Readline\Autocompleter\Path::getWordDefinition ( )

Get definition of a word.

Returns
string

Implements Hoa\Console\Readline\Autocompleter\Autocompleter.

Definition at line 164 of file Path.php.

165  {
166  return '/?[\w\d\\_\-\.]+(/[\w\d\\_\-\.]*)*';
167  }
Hoa\Console\Readline\Autocompleter\Path::setIteratorFactory ( \Closure  $iteratorFactory)

Set iterator factory (a finder).

Parameters
\Closure$iteratorFactoryClosore with a single argument: $path of the iterator.
Returns
string

Definition at line 200 of file Path.php.

201  {
203  $this->_iteratorFactory = $iteratorFactory;
204 
205  return $old;
206  }

Here is the caller graph for this function:

Hoa\Console\Readline\Autocompleter\Path::setRoot (   $root)

Set root.

Parameters
string$rootRoot.
Returns
string

Definition at line 175 of file Path.php.

176  {
177  $old = $this->_root;
178  $this->_root = $root;
179 
180  return $old;
181  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Console\Readline\Autocompleter\Path::$_iteratorFactory = null
protected

Definition at line 68 of file Path.php.

Hoa\Console\Readline\Autocompleter\Path::$_root = null
protected

Definition at line 61 of file Path.php.

const Hoa\Console\Readline\Autocompleter\Path::PWD = null

Root is the current working directory.

string

Definition at line 54 of file Path.php.


The documentation for this class was generated from the following file: