/**
  * Tests PEAR-like class names beginning with underscore, or with a double
  * underscore in between.
  */
 function testSpecialUnderscores()
 {
     // Prepare the class finder.
     $finder = new ClassFinder();
     $finder->registerPrefixDeep('_ex_ample', 'test://lib');
     $finder->registerPrefixRoot('_ex_ample', 'test://vendor');
     // Verify that underscores are not a problem..
     $this->assertCandidateOrder($finder, '_ex_ample_Abc%_Def', array('test://lib/Abc%/Def.php', 'test://vendor/_ex/ample/Abc%/Def.php'));
     $this->assertCandidateOrder($finder, '_abc_Foo%', array());
     $this->assertCandidateOrder($finder, 'abc__Foo%', array());
 }
 /**
  * @param ClassFinder $finder
  * @param string $class
  * @param array $expectedSuggestions
  *
  * @return bool
  *   Result of the assertion
  */
 protected function assertFinderSuggestions($finder, $class, array $expectedSuggestions)
 {
     $success = TRUE;
     for ($iAccept = 0; $iAccept < count($expectedSuggestions); ++$iAccept) {
         list($method_name, $file) = $expectedSuggestions[$iAccept];
         $api = new CollectFilesInjectedApi($class, $method_name, $file);
         $finder->apiFindFile($api, $class);
         $suggestions = $api->getSuggestions();
         $expected = array_slice($expectedSuggestions, 0, $iAccept + 1);
         $success = $success && $this->assertEqualBlock($expected, $suggestions, "Finder suggestions for class <code>{$class}</code>:");
     }
     return $success;
 }
 function testComposerJson()
 {
     $finder = new ClassFinder();
     $masterAdapter = new ClassFinderAdapter($finder, new ClassMapGenerator());
     foreach (array(dirname(__DIR__) . '/fixtures/.libraries/ComposerTestLib' => array('ComposerTestLib\\Foo', 'ComposerTestLib\\Other\\Foo'), dirname(__DIR__) . '/fixtures/.libraries/ComposerTargetDirTestLib' => array('Acme\\ComposerTargetDirTestLib\\Foo')) as $dir => $classes) {
         $localDirectoryAdapter = new LocalDirectoryAdapter($masterAdapter, $dir);
         $localDirectoryAdapter->composerJson('composer.json');
         foreach ($classes as $class) {
             $this->assertFalse(class_exists($class, FALSE), "Class {$class} not defined yet.");
             $finder->loadClass($class);
             $this->assertTrue(class_exists($class, FALSE), "Class {$class} is defined.");
         }
     }
 }
 public static function listClasses($dirname = ".")
 {
     if (self::$cache == NULL) {
         self::$cache = self::readDirectory($dirname);
     }
     return self::$cache;
 }
Exemple #5
0
 public function findCommands()
 {
     $commandsList = new CommandsList();
     // Find all classes in php files whose paths contain "PhixCommands":
     $classFinder = new ClassFinder();
     $classFinder->addFoldersToSearch($this->foldersToSearch);
     $classFinder->setFileRegex('/PhixCommands.*\\.php$/');
     $classes = $classFinder->findClassFiles();
     foreach ($classes as $newClass => $filename) {
         include_once $filename;
         if ($this->testIsPhixCommand($newClass)) {
             // we have a winner!
             $commandsList->addClass($newClass);
         }
     }
     return $commandsList;
 }
Exemple #6
0
 public function testSearchesForClasses()
 {
     $obj = new ClassFinder();
     $folderToSearch = realpath(__DIR__ . '/../..');
     $obj->addFolderToSearch($folderToSearch);
     $classes = $obj->findClassFiles();
     $expectedClasses = array('Phix_Project\\PhixCommands\\HelpCommandTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixCommands' . DIRECTORY_SEPARATOR . 'HelpCommandTest.php', 'Phix_Project\\PhixExtensions\\CommandBaseTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixExtensions' . DIRECTORY_SEPARATOR . 'CommandBaseTest.php', 'Phix_Project\\PhixExtensions\\DummyCommand' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixExtensions' . DIRECTORY_SEPARATOR . 'CommandBaseTest.php', 'Phix_Project\\PhixExtensions\\DummyCommandWithNoSwitches' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixExtensions' . DIRECTORY_SEPARATOR . 'CommandBaseTest.php', 'Phix_Project\\PhixExtensions\\DummyCommandWithNoSwitchesAndOneArg' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixExtensions' . DIRECTORY_SEPARATOR . 'CommandBaseTest.php', 'Phix_Project\\PhixExtensions\\DummyCommandWithSwitches' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixExtensions' . DIRECTORY_SEPARATOR . 'CommandBaseTest.php', 'Phix_Project\\PhixSwitches\\DebugSwitchTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'DebugSwitchTest.php', 'Phix_Project\\PhixSwitches\\IncludeSwitchTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'IncludeSwitchTest.php', 'Phix_Project\\PhixSwitches\\LongHelpSwitchTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'LongHelpSwitchTest.php', 'Phix_Project\\PhixSwitches\\PhixSwitchesTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'PhixSwitchesTest.php', 'Phix_Project\\PhixSwitches\\ShortHelpSwitchTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'ShortHelpSwitchTest.php', 'Phix_Project\\PhixSwitches\\SwitchBaseTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'SwitchBaseTest.php', 'Phix_Project\\PhixSwitches\\VersionSwitchTest' => $folderToSearch . DIRECTORY_SEPARATOR . 'Phix_Project' . DIRECTORY_SEPARATOR . 'PhixSwitches' . DIRECTORY_SEPARATOR . 'VersionSwitchTest.php', 'Phix_Project\\Phix\\ClassFinderTest' => __DIR__ . DIRECTORY_SEPARATOR . 'ClassFinderTest.php', 'Phix_Project\\Phix\\CommandsFinderTest' => __DIR__ . DIRECTORY_SEPARATOR . 'CommandsFinderTest.php', 'Phix_Project\\Phix\\CommandsListTest' => __DIR__ . DIRECTORY_SEPARATOR . 'CommandsListTest.php', 'Phix_Project\\Phix\\ContextTest' => __DIR__ . DIRECTORY_SEPARATOR . 'ContextTest.php', 'Phix_Project\\Phix\\DummyCommand' => __DIR__ . DIRECTORY_SEPARATOR . 'CommandsListTest.php', 'Phix_Project\\Phix\\DummyNotACommand' => __DIR__ . DIRECTORY_SEPARATOR . 'CommandsListTest.php');
     $this->assertEquals($expectedClasses, $classes);
 }
 /**
  * Find Classes
  *
  * @access public
  * @param string $subDir
  * @param string $suffix
  * @param string $parent
  * @param boolean $reflection
  * @return array
  */
 public function findClasses($subDir = null, $suffix = null, $parent = null, $reflection = false)
 {
     $classes = [];
     foreach ($this->locations as $namespace => $location) {
         $this->setRootDirectory($location);
         $this->setRootNamespace($namespace);
         $classes = array_merge($classes, parent::findClasses($subDir, $suffix, $parent, $reflection));
     }
     return $classes;
 }
Exemple #8
0
 /**
  * @param Block $block
  * @return bool
  */
 public function getBlock($block)
 {
     if (!$this->appInstance->backendClient) {
         return false;
     }
     if ($this->upstream instanceof BackendServerConnection) {
         return false;
     }
     if (ClassFinder::getClassBasename($block) === 'Block') {
         return false;
     }
     /**
      * @param BackendClientConnection $conn
      */
     $fc = function ($conn) use($block) {
         if (!$conn->connected) {
             // fail
             return;
         }
         if (!$this->backendClientConn) {
             $this->backendClientConn = $conn;
             $conn->beginRequest($this);
         }
         $conn->getBlock($this->rid, $block);
         if ($this->backendClientCbs !== null) {
             $this->backendClientCbs->executeAll($conn);
             $this->backendClientCbs = null;
         }
     };
     if ($this->backendClientConn) {
         $this->backendClientConn->onConnected($fc);
     } else {
         if ($this->backendClientInited) {
             if ($this->backendClientCbs === null) {
                 $this->backendClientCbs = new StackCallbacks();
             }
             $this->backendClientCbs->push($fc);
         } else {
             $this->appInstance->backendClient->getConnection($fc);
             $this->backendClientInited = true;
         }
     }
     return true;
 }
Exemple #9
0
 /**
  * Constructor.
  * @return void
  */
 protected function init()
 {
     if ($this->isEnabled()) {
         list($class, $name) = explode(':', $this->name . ':');
         $realclass = ClassFinder::find($class);
         $e = explode('\\', $realclass);
         if ($e[sizeof($e) - 1] !== 'Pool' && class_exists($realclass . '\\Pool')) {
             $realclass .= '\\Pool';
         }
         if ($realclass !== $class) {
             $base = '\\PHPDaemon\\Core\\Pool:';
             Daemon::$config->renameSection($base . $class . ($name !== '' ? ':' . $name : ''), $base . $realclass . ($name !== '' ? ':' . $name : ''));
         }
         if (!class_exists($realclass)) {
             Daemon::log($realclass . ' class not exists.');
             return;
         }
         $func = [$realclass, 'getInstance'];
         $this->pool = $func($name);
         $this->pool->appInstance = $this;
     }
 }
Exemple #10
0
 /**
  * Helper to get instance of AutoloadBuilder with cli options applied
  *
  * @throws \RuntimeException
  * @return \TheSeer\Autoload\AutoloadBuilder|\TheSeer\Autoload\StaticBuilder
  */
 public function getBuilder(ClassFinder $finder)
 {
     $isStatic = $this->config->isStaticMode();
     $isPhar = $this->config->isPharMode();
     $isCompat = $this->config->isCompatMode();
     $noLower = !$this->config->isLowercaseMode();
     $isOnce = $this->config->isOnceMode();
     $tplType = $noLower ? 'cs' : 'ci';
     if ($isStatic === TRUE) {
         $builder = new StaticBuilder($finder->getMerged());
         $builder->setDependencies($finder->getDependencies());
         $builder->setPharMode($isPhar);
         $builder->setRequireOnce($isOnce);
     } else {
         $builder = new AutoloadBuilder($finder->getMerged());
     }
     $builder->setCompat($isCompat);
     $basedir = $this->config->getBaseDirectory();
     if (!$basedir || !is_dir($basedir)) {
         throw new \RuntimeException("Given basedir '{$basedir}' does not exist or is not a directory");
     }
     $builder->setBaseDir($basedir);
     $template = $this->config->getTemplate();
     if ($template !== NULL) {
         if (!file_exists($template)) {
             $alternative = __DIR__ . '/templates/' . $tplType . '/' . $template;
             if (file_exists($alternative)) {
                 $template = $alternative;
             }
         }
         $builder->setTemplateFile($template);
     } else {
         // determine auto template to use
         $tplFile = 'default.php.tpl';
         if ($isCompat) {
             $tplFile = 'php52.php.tpl';
         }
         if ($isPhar) {
             if ($isStatic) {
                 $tplFile = 'staticphar.php.tpl';
             } else {
                 $tplFile = 'phar.php.tpl';
             }
         } elseif ($isStatic) {
             $tplFile = 'static.php.tpl';
             $tplType = '.';
         }
         $builder->setTemplateFile(__DIR__ . '/templates/' . $tplType . '/' . $tplFile);
     }
     $format = $this->config->getDateFormat();
     if ($format) {
         $builder->setDateTimeFormat($format);
     }
     $builder->setIndent($this->config->getIndent());
     $builder->setLineBreak($this->config->getLinebreak());
     foreach ($this->config->getVariables() as $name => $value) {
         $builder->setVariable($name, $value);
     }
     return $builder;
 }
 /**
  * 初始化class finder上下文
  * @param string $project_path
  */
 private function initContext($project_path)
 {
     $finder = new ClassFinder($project_path);
     $finder->getContext();
 }
 /**
  * Main executor method
  *
  * @return void
  */
 public function run()
 {
     $input = new \ezcConsoleInput();
     $versionOption = $input->registerOption(new \ezcConsoleOption('v', 'version'));
     $versionOption->shorthelp = 'Prints the version and exits';
     $versionOption->isHelpOption = true;
     $helpOption = $input->registerOption(new \ezcConsoleOption('h', 'help'));
     $helpOption->isHelpOption = true;
     $helpOption->shorthelp = 'Prints this usage information';
     $outputOption = $input->registerOption(new \ezcConsoleOption('o', 'output', \ezcConsoleInput::TYPE_STRING, 'STDOUT', false, 'Output file for generated code (default: STDOUT)'));
     $pharOption = $input->registerOption(new \ezcConsoleOption('p', 'phar', \ezcConsoleInput::TYPE_NONE, null, false, 'Build a phar archive of directory contents', null, array(new \ezcConsoleOptionRule($input->getOption('o')))));
     $input->registerOption(new \ezcConsoleOption('i', 'include', \ezcConsoleInput::TYPE_STRING, '*.php', true, 'File pattern to include (default: *.php)'));
     $input->registerOption(new \ezcConsoleOption('e', 'exclude', \ezcConsoleInput::TYPE_STRING, null, true, 'File pattern to exclude'));
     $input->registerOption(new \ezcConsoleOption('b', 'basedir', \ezcConsoleInput::TYPE_STRING, null, false, 'Basedir for filepaths'));
     $input->registerOption(new \ezcConsoleOption('t', 'template', \ezcConsoleInput::TYPE_STRING, null, false, 'Path to code template to use'));
     $input->registerOption(new \ezcConsoleOption('', 'format', \ezcConsoleInput::TYPE_STRING, null, false, 'Dateformat string for timestamp'));
     $input->registerOption(new \ezcConsoleOption('', 'linebreak', \ezcConsoleInput::TYPE_STRING, null, false, 'Linebreak style (CR, CR/LF or LF)'));
     $input->registerOption(new \ezcConsoleOption('', 'indent', \ezcConsoleInput::TYPE_STRING, null, false, 'String used for indenting (default: 3 spaces)'));
     $lintOption = $input->registerOption(new \ezcConsoleOption('', 'lint', \ezcConsoleInput::TYPE_NONE, null, false, 'Run lint on generated code'));
     $input->registerOption(new \ezcConsoleOption('', 'lint-php', \ezcConsoleInput::TYPE_STRING, null, false, 'PHP binary path for linting (default: /usr/bin/php or c:\\php\\php.exe)'));
     $input->argumentDefinition = new \ezcConsoleArguments();
     $input->argumentDefinition[0] = new \ezcConsoleArgument("directory");
     $input->argumentDefinition[0]->shorthelp = "The directory to process.";
     try {
         $input->process();
     } catch (\ezcConsoleException $e) {
         echo $e->getMessage() . "\n\n";
         $this->showVersion();
         $this->showUsage();
         exit(3);
     }
     if ($helpOption->value === true) {
         $this->showVersion();
         $this->showUsage();
         exit(0);
     }
     if ($versionOption->value === true) {
         $this->showVersion();
         exit(0);
     }
     try {
         $scanner = $this->getScanner($input);
         if ($pharOption->value !== false) {
             $phar = $this->buildPhar($scanner, $input);
             $scanner->rewind();
         }
         $finder = new ClassFinder();
         $found = $finder->parseMulti($scanner);
         // this unset is needed to "fix" a segfault on shutdown
         unset($scanner);
         $builder = $this->getBuilder($found, $input);
         if ($lintOption->value === true) {
             exit($this->lintCode($builder->render(), $input) ? 0 : 4);
         }
         if ($outputOption->value == 'STDOUT') {
             echo $builder->render();
         } else {
             if ($pharOption->value !== false) {
                 $builder->setVariable('PHAR', basename($outputOption->value));
                 $phar->setStub($builder->render());
                 $phar->stopBuffering();
                 echo "phar archive '{$outputOption->value}' generated.\n\n";
             } else {
                 $builder->save($outputOption->value);
                 echo "Autoload file '{$outputOption->value}' generated.\n\n";
             }
         }
         exit(0);
     } catch (\Exception $e) {
         fwrite(STDERR, "Error while processing request:\n");
         fwrite(STDERR, ' - ' . $e->getMessage() . "\n");
         exit(1);
     }
 }