Skip to content

Instantly share code, notes, and snippets.

@uwej711
Created June 21, 2013 15:15
Show Gist options
  • Select an option

  • Save uwej711/5831888 to your computer and use it in GitHub Desktop.

Select an option

Save uwej711/5831888 to your computer and use it in GitHub Desktop.
Sample Filesystem-Browser for the TreeBrowserBundle
<?php
/**
* www.valiton.com
*
* @author Uwe Jäger <uwe.jaeger@valiton.com>
*/
namespace Valiton\FileBrowserBundle\Tree;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Cmf\Bundle\TreeBrowserBundle\Tree\TreeInterface;
/**
* A TreeInterface implementation for the file system
*/
class FileSystemTree implements TreeInterface
{
/** @var string */
protected $root;
/** @var Filesystem */
protected $filesystem;
/** @var \Symfony\Component\Templating\Helper\CoreAssetsHelper */
protected $assetHelper;
/**
* @param string $root
* @param ContainerInterface $container
*/
public function __construct($root, ContainerInterface $container)
{
$this->root = $root;
$this->filesystem = $container->get('filesystem');
$this->assetHelper = $container->get('templating.helper.assets');
}
/**
* Returns an array representation of children nodes of a node
*
* @param string $path The path of any PHPCR node
* @return array children list
*/
public function getChildren($path)
{
$children = array();
$iterator = new \DirectoryIterator($this->getRealPath($path));
foreach ($iterator as $info) {
if (!$info->isDot())
$children[] = $this->fileInfoToArray($info);
}
return $children;
}
/**
* Move the node at $path to $target
*
* @param string $path
* @param string $target
* @return array the new id of the moved node at $target and the url_safe_id
*/
public function move($path, $target)
{
$this->filesystem->rename($this->getRealPath($path), $this->getRealPath($target) . '/' . basename($path));
return array(
'id' => $target . '/' . basename($path),
'url_safe_id' => ltrim($target, '/') . '/' . basename($path)
);
}
/**
* Reorder $moved (child of $parent) before or after $target
*
* @param string $parent the id of the parent
* @param string $moved the id of the child being moved
* @param string $target the id of the target node
* @param bool $before insert before or after the target
* @return void
*/
public function reorder($parent, $moved, $target, $before)
{
// NOOP
}
/**
* Get the id of the initially selected node
*
* @return string
*/
public function getSelectedNode()
{
return "/";
}
/**
* Get the id of the root node
*
* @return string
*/
public function getRootNode()
{
return "/";
}
/**
* Convert the file info to an array
*
* @param \SplFileInfo $info
* @return array
*/
protected function fileInfoToArray(\SplFileInfo $info)
{
$iPath = $info->getPath();
$iFilename = $info->getFilename();
$path = substr($iPath, strlen($this->root), strlen($iPath));
$id = $path . '/' . $iFilename;
return array(
'data' => $iFilename,
'attr' => array(
'id' => $id,
'url_safe_id' => ltrim($id, '/'),
'rel' => $info->isDir() ? 'folder' : 'document'
),
'state' => $info->isDir() ? 'closed' : null
);
}
/**
* Get the alias for this tree
*
* @return string
*/
public function getAlias()
{
return 'file_tree';
}
/**
* Get an array describing the available node types
*
* @return array
*/
public function getNodeTypes()
{
return array(
'folder' => array(
'icon' => array('image' => $this->assetHelper->getUrl('bundles/symfonycmftree/images/folder.png')),
'valid_children' => array('folder', 'document'),
'label' => 'Ordner',
'routes' => array(
'create_route' => 'valiton_file_browser_create_folder',
'select_route' => 'valiton_file_browser_detail',
'delete_route' => 'valiton_file_browser_delete'
)
),
'document' => array(
'icon' => array('image' => $this->assetHelper->getUrl('bundles/symfonycmftree/images/document.png')),
'valid_children' => 'none',
'label' => 'Datei',
'routes' => array(
'create_route' => 'valiton_file_browser_create_document',
'select_route' => 'valiton_file_browser_detail',
'delete_route' => 'valiton_file_browser_delete'
)
)
);
}
/**
* Get an array for labels.
*
* @return array
*/
public function getLabels()
{
return array(
'createItem' => 'Anlegen',
'deleteItem' => 'Löschen'
);
}
public function createFolderAt($path)
{
$file = tempnam($this->getRealPath($path), 'Tree');
unlink($file);
mkdir($file);
}
public function createFileAt($path)
{
tempnam($this->getRealPath($path), 'Tree');
}
public function delete($path)
{
$this->filesystem->remove($this->getRealPath($path));
}
public function getProperties($path)
{
$info = new \SplFileInfo($this->getRealPath($path));
return array(
'Name' => $info->getFilename(),
'Size' => $info->getSize(),
'Created' => $info->getCTime()
);
}
protected function getRealPath($path)
{
return $this->root . '/'. ltrim($path, '/');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment