Skip to content

Instantly share code, notes, and snippets.

@asika32764
Last active December 23, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asika32764/6576172 to your computer and use it in GitHub Desktop.
Save asika32764/6576172 to your computer and use it in GitHub Desktop.
<?php
$path = 'src/Component/Todo/View/../..';
$dl = new \DirectoryLocator($path);
// __toString
// -----------------------------
echo $dl; // src/Component
// Get child directory
// -----------------------------
echo $dl->child('Todo'); // src/Component/Todo
echo $dl->child('View/Items'); // src/Component/Todo/View/Items
// Get child directories by chaining
echo $dl->child('Template')->child('Twig'); // src/Component/Todo/View/Items/Template/Twig
// Get parent directory
// -----------------------------
echo $dl->parent(); // src/Component/Todo/View/Items/Template
echo $dl->parent()->parent() or $dl->parent(2); // src/Component/Todo/View
// Find parent
echo $dl->parent('Todo'); // src/Component/Todo
// Other methods
// -----------------------------
echo $dl->addPrefix(JPATH_ROOT); // /var/www/joomla/src/Component/Todo
echo $dl->isDir(); // true or false
echo $dl->isFile(); // true or false
// Get Filesystem Object
// -----------------------------
$dl->getFolders(); // return DirectoryIterator(CURRENT_PATH)
$dl->getFiles(); // return FilesystemIterator
$dl->getInfo(); // return SplFileInfo of current directory
$dl->child('www')->getInfo('index.php'); // return SplFileInfo of this file
// Strict Mode
// -----------------------------
$dl2 = new \DirectoryLocator(JPATH_ROOT . '/src', true);
$dls->child('Campenont'); // throw PathNotExistsException();
$dls->child('../www/index.php'); // throw PathNotDirException();
// DirectoryCollection
// -----------------------------
$dc = new \DirectoryCollection(
new \DirectoryLocator($path1),
new \DirectoryLocator($path2),
new \DirectoryLocator($path3)
);
// Add paths
$dc->addPath(new \DirectoryLocator($path4));
$dc->addPaths(array(new \DirectoryLocator($path5)));
// foreach
foreach($dc as $dl)
{
echo $dl;
}
// Use it as array or string
$cache = new \DirectoryLocator(JPATH_ROOT . '/cache');
$loader = new \Twig_Loader_Filesystem($dc);
$twig = new \Twig_Environment($loader, array(
'cache' => (string) $cache,
));
// Add prefix to all
$dc->addPrefix(JPATH_ROOT); // this method will adds prefix to every paths in this ArrayObject
// Find files
$dc->find('config.json');
$dc->findAll('config_*.json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment