protected function getFiles() { $files = array('LICENSE', 'src/autoload.php', 'src/vendor/symfony/src/Symfony/Foundation/UniversalClassLoader.php', 'src/vendor/zend/library/Zend/Exception.php', 'src/vendor/zend/library/Zend/Uri.php', 'src/vendor/zend/library/Zend/Validate.php', 'src/vendor/zend/library/Zend/Validate/Abstract.php', 'src/vendor/zend/library/Zend/Validate/Interface.php', 'src/vendor/zend/library/Zend/Validate/Hostname.php', 'src/vendor/zend/library/Zend/Validate/Ip.php', 'src/vendor/zend/library/Zend/Validate/Hostname/Com.php', 'src/vendor/zend/library/Zend/Validate/Hostname/Jp.php'); $dirs = array('src/Goutte', 'src/vendor/symfony/src/Symfony/Components/BrowserKit', 'src/vendor/symfony/src/Symfony/Components/DomCrawler', 'src/vendor/symfony/src/Symfony/Components/CssSelector', 'src/vendor/symfony/src/Symfony/Components/Process', 'src/vendor/zend/library/Zend/Uri', 'src/vendor/zend/library/Zend/Http'); $finder = new Finder(); $iterator = $finder->files()->name('*.php')->in($dirs); return array_merge($files, iterator_to_array($iterator)); }
/** * Creates a Kernel. * * If you run tests with the PHPUnit CLI tool, everything will work as expected. * If not, override this method in your test classes. * * Available options: * * * environment * * debug * * @param array $options An array of options * * @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance */ protected function createKernel(array $options = array()) { // black magic below, you have been warned! $dir = getcwd(); if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) { throw new \RuntimeException('You must override the WebTestCase::createKernel() method.'); } // find the --configuration flag from PHPUnit $cli = implode(' ', $_SERVER['argv']); if (preg_match('/\\-\\-configuration[= ]+([^ ]+)/', $cli, $matches)) { $dir = $dir . '/' . dirname($matches[1]); } $finder = new Finder(); $finder->name('*Kernel.php')->in($dir); if (!count($finder)) { throw new \RuntimeException('You must override the WebTestCase::createKernel() method.'); } $file = current(iterator_to_array($finder)); $class = $file->getBasename('.php'); unset($finder); require_once $file; return new $class(isset($options['environment']) ? $options['environment'] : 'test', isset($options['debug']) ? $debug : true); }
protected function callPhing($taskName, $properties = array()) { $kernel = $this->application->getKernel(); $tmpDir = sys_get_temp_dir() . '/propel-gen'; $filesystem = new Filesystem(); $filesystem->remove($tmpDir); $filesystem->mkdirs($tmpDir); foreach ($kernel->getBundles() as $bundle) { if (is_dir($dir = $bundle->getPath() . '/Resources/config')) { $finder = new Finder(); $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir); $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath())); $prefix = implode('.', array_slice($parts, 1, -2)); foreach ($schemas as $schema) { $tempSchema = md5($schema) . '_' . $schema->getBaseName(); $this->tempSchemas[$tempSchema] = array('bundle' => $bundle->getName(), 'basename' => $schema->getBaseName(), 'path' => $schema->getPathname()); $file = $tmpDir . DIRECTORY_SEPARATOR . $tempSchema; $filesystem->copy((string) $schema, $file); // the package needs to be set absolute // besides, the automated namespace to package conversion has not taken place yet // so it needs to be done manually $database = simplexml_load_file($file); if (isset($database['package'])) { $database['package'] = $prefix . '.' . $database['package']; } elseif (isset($database['namespace'])) { $database['package'] = $prefix . '.' . str_replace('\\', '.', $database['namespace']); } foreach ($database->table as $table) { if (isset($table['package'])) { $table['package'] = $prefix . '.' . $table['package']; } elseif (isset($table['namespace'])) { $table['package'] = $prefix . '.' . str_replace('\\', '.', $table['namespace']); } } file_put_contents($file, $database->asXML()); } } } $filesystem->touch($tmpDir . '/build.properties'); $args = array(); // $bufferPhingOutput = !$this->commandApplication->withTrace(); $properties = array_merge(array('propel.database' => 'mysql', 'project.dir' => $tmpDir, 'propel.output.dir' => $kernel->getRootDir() . '/propel', 'propel.php.dir' => '/', 'propel.packageObjectModel' => true), $properties); foreach ($properties as $key => $value) { $args[] = "-D{$key}={$value}"; } // Build file $args[] = '-f'; $args[] = realpath($kernel->getContainer()->getParameter('propel.path') . '/generator/build.xml'); /* // Logger if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT))) { $args[] = '-logger'; $args[] = 'phing.listener.AnsiColorLogger'; } // Add our listener to detect errors $args[] = '-listener'; $args[] = 'sfPhingListener'; */ // Add any arbitrary arguments last foreach ($this->additionalPhingArgs as $arg) { if (in_array($arg, array('verbose', 'debug'))) { $bufferPhingOutput = false; } $args[] = '-' . $arg; } $args[] = $taskName; // enable output buffering Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w'))); Phing::startup(); Phing::setProperty('phing.home', getenv('PHING_HOME')); // $this->logSection('propel', 'Running "'.$taskName.'" phing task'); $bufferPhingOutput = false; if ($bufferPhingOutput) { ob_start(); } $m = new Phing(); $m->execute($args); $m->runBuild(); if ($bufferPhingOutput) { ob_end_clean(); } print $bufferPhingOutput; chdir($kernel->getRootDir()); /* // any errors? $ret = true; if (sfPhingListener::hasErrors()) { $messages = array('Some problems occurred when executing the task:'); foreach (sfPhingListener::getExceptions() as $exception) { $messages[] = ''; $messages[] = preg_replace('/^.*build\-propel\.xml/', 'build-propel.xml', $exception->getMessage()); $messages[] = ''; } if (count(sfPhingListener::getErrors())) { $messages[] = 'If the exception message is not clear enough, read the output of the task for'; $messages[] = 'more information'; } $this->logBlock($messages, 'ERROR_LARGE'); $ret = false; } */ $ret = true; return $ret; }
protected function execute(InputInterface $input, OutputInterface $output) { $defaultEm = $this->container->getDoctrine_ORM_EntityManagerService(); $dirOrFile = $input->getOption('fixtures'); if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } else { $paths = array(); $bundleDirs = $this->container->getKernelService()->getBundleDirs(); foreach ($this->container->getKernelService()->getBundles() as $bundle) { $tmp = dirname(str_replace('\\', '/', get_class($bundle))); $namespace = str_replace('/', '\\', dirname($tmp)); $class = basename($tmp); if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace] . '/' . $class . '/Resources/data/fixtures/doctrine')) { $paths[] = $dir; } } } $files = array(); foreach ($paths as $path) { if (is_dir($path)) { $finder = new Finder(); $found = iterator_to_array($finder->files()->name('*.php')->in($path)); } else { $found = array($path); } $files = array_merge($files, $found); } $ems = array(); $emEntities = array(); $files = array_unique($files); foreach ($files as $file) { $em = $defaultEm; $output->writeln(sprintf('<info>Loading data fixtures from <comment>"%s"</comment></info>', $file)); $before = array_keys(get_defined_vars()); include $file; $after = array_keys(get_defined_vars()); $new = array_diff($after, $before); $params = $em->getConnection()->getParams(); $emName = isset($params['path']) ? $params['path'] : $params['dbname']; $ems[$emName] = $em; $emEntities[$emName] = array(); $variables = array_values($new); foreach ($variables as $variable) { $value = ${$variable}; if (!is_object($value) || $value instanceof \Doctrine\ORM\EntityManager) { continue; } $emEntities[$emName][] = $value; } foreach ($ems as $emName => $em) { if (!$input->getOption('append')) { $output->writeln(sprintf('<info>Purging data from entity manager named <comment>"%s"</comment></info>', $emName)); $this->purgeEntityManager($em); } $entities = $emEntities[$emName]; $numEntities = count($entities); $output->writeln(sprintf('<info>Persisting "%s" ' . ($numEntities > 1 ? 'entities' : 'entity') . '</info>', count($entities))); foreach ($entities as $entity) { $output->writeln(sprintf('<info>Persisting "%s" entity:</info>', get_class($entity))); $output->writeln(''); $output->writeln(var_dump($entity)); $em->persist($entity); } $output->writeln('<info>Flushing entity manager</info>'); $em->flush(); } } }
/** * 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)); } } } } }
public function testGetIterator() { $finder = new Finder(); try { $finder->getIterator(); $this->fail('->getIterator() throws a \\LogicException if the in() method has not been called'); } catch (\Exception $e) { $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \\LogicException if the in() method has not been called'); } $finder = new Finder(); $dirs = array(); foreach ($finder->directories()->in(self::$tmpDir) as $dir) { $dirs[] = (string) $dir; } $this->assertEquals($this->toAbsolute(array('foo', 'toto')), $dirs, 'implements the \\IteratorAggregate interface'); $finder = new Finder(); $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \\IteratorAggregate interface'); $finder = new Finder(); $a = iterator_to_array($finder->directories()->in(self::$tmpDir)); $this->assertEquals($this->toAbsolute(array('foo', 'toto')), array_values(array_map(function ($a) { return (string) $a; }, $a)), 'implements the \\IteratorAggregate interface'); }
/** * @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; }
/** * Finds and registers commands for the current bundle. * * @param Symfony\Components\Console\Application $application An Application instance */ public function registerCommands(Application $application) { if (!($dir = realpath($this->getPath() . '/Command'))) { return; } $finder = new Finder(); $finder->files()->name('*Command.php')->in($dir); $prefix = $this->namespacePrefix . '\\' . $this->name . '\\Command'; foreach ($finder as $file) { $r = new \ReflectionClass($prefix . strtr($file->getPath(), array($dir => '', '/' => '\\')) . '\\' . basename($file, '.php')); if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) { $application->addCommand($r->newInstance()); } } }