示例#1
0
 public function testIn()
 {
     $finder = new Finder();
     try {
         $finder->in('foobar');
         $this->fail('->in() throws a \\InvalidArgumentException if the directory does not exist');
     } catch (\Exception $e) {
         $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \\InvalidArgumentException if the directory does not exist');
     }
     $finder = new Finder();
     $iterator = $finder->files()->name('*.php')->maxDepth(0)->in(array(self::$tmpDir, __DIR__))->getIterator();
     $this->assertIterator(array(self::$tmpDir . 'test.php', __DIR__ . '/FinderTest.php', __DIR__ . '/GlobTest.php', __DIR__ . '/NumberCompareTest.php'), $iterator);
 }
示例#2
0
 /**
  * Mirrors a directory to another.
  *
  * @param string $originDir  The origin directory
  * @param string $targetDir  The target directory
  * @param Finder $finder     An Finder instance
  * @param array  $options    An array of options (see copy())
  *
  * @throws \RuntimeException When file type is unknown
  */
 public function mirror($originDir, $targetDir, Finder $finder = null, $options = array())
 {
     if (null === $finder) {
         $finder = new Finder();
     }
     foreach ($finder->in($originDir) as $file) {
         $target = $targetDir . DIRECTORY_SEPARATOR . str_replace(realpath($originDir), '', $file->getRealPath());
         if (is_dir($file)) {
             $this->mkdirs($target);
         } else {
             if (is_file($file)) {
                 $this->copy($file, $target, $options);
             } else {
                 if (is_link($file)) {
                     $this->symlink($file, $target);
                 } else {
                     throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file));
                 }
             }
         }
     }
 }
示例#3
0
    /**
     * @param Symfony\Components\EventDispatcher\Event $event
     *
     * @see Symfony\Components\EventDispatcher\EventDispatcher::notifyUntil()
     */
    public function handle(Event $event)
    {
        $request = $event->getSubject();
        $url = trim($request->getRequestUrl(), '/');
        $name = basename($url);
        $dir = trim(substr($url, 0, strlen($url) - strlen($name)), '/');
        $path = $this->documentRoot . '/' . ($dir == $name ? $name : $dir . '/' . $name);
        $path = realpath($path);
        // skip document root
        if ($path == $this->documentRoot) {
            return false;
        }
        // path hacking
        if (substr($path, 0, strlen($this->documentRoot)) !== $this->documentRoot) {
            return false;
        }
        $headers = array();
        // add Date header
        $date = new \DateTime();
        $headers['Date'] = $date->format(DATE_RFC822);
        $path = new \SplFileInfo($path);
        // is file
        if ($path->isFile()) {
            // get mime type
            $info = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($info, $path);
            finfo_close($info);
            // get mime encoding
            $info = finfo_open(FILEINFO_MIME_ENCODING);
            $encoding = finfo_file($info, $path);
            finfo_close($info);
            // add Content-Type/Encoding header
            $headers['Content-Type'] = $mime;
            $headers['Content-Encoding'] = $encoding;
            // add Content-Length header
            $headers['Content-Length'] = filesize($path);
            // build Response
            $response = $this->container->getServer_ResponseService();
            $response->setHttpVersion($request->getHttpVersion());
            $response->setStatusCode(200);
            $response->addHeaders($headers);
            $response->setBody(file_get_contents($path));
            $event->setReturnValue($response);
            return true;
        }
        // is dir
        if ($path->isDir()) {
            $dir = trim(substr($path, strlen($this->documentRoot)), '/');
            $finder = new Finder();
            $finder->depth(0);
            // @TODO add ExceptionHandler-like rendering
            $item = <<<EOF
<tr>
<td valign="top">%s</td>
<td valign="top"><a href="/%s">%s</a></td>
<td align="right">%s</td>
<td align="right">%s%s</td>
</tr>
EOF;
            $list = array();
            if ($path != $this->documentRoot) {
                $list[] = sprintf($item, 'parent', $dir . '/../', 'Parent directory', '', '-', '');
            }
            foreach ($finder->in($path) as $file) {
                $date = new \DateTime();
                $date->setTimestamp($file->getMTime());
                $list[] = sprintf($item, $file->getType(), ltrim(substr($file->getRealpath(), strlen($this->documentRoot)), '/'), $file->getFilename(), $date->format('d-M-Y H:i'), $file->getSize(), '');
            }
            $layout = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Index of /{$dir}</title>
 </head>
 <body>
<h1>Index of /{$dir}</h1>
<table><thead>
<tr><th></th><th>Name</th><th>Last modified</th><th>Size</th></tr>
</thead>
<tbody>%s</tbody>
</table>
</body></html>
EOF;
            $content = sprintf($layout, implode("\n", $list));
            // add Content-Type header
            $headers['Content-Type'] = 'text/html; charset=UTF-8';
            // add Content-Length header
            $headers['Content-Length'] = strlen($content);
            // build Response
            $response = $this->container->getServer_ResponseService();
            $response->setHttpVersion($request->getHttpVersion());
            $response->setStatusCode(200);
            $response->addHeaders($headers);
            $response->setBody($content);
            $event->setReturnValue($response);
            return true;
        }
        return false;
    }