Esempio n. 1
0
 /**
  * @param  InputInterface $input
  * @return ClassIterator
  */
 public function createClassIterator(InputInterface $input)
 {
     $classIterator = new ClassIterator($this->createFinder($input));
     $classIterator->enableAutoloading();
     foreach ($input->getOption('type') as $pattern) {
         $classIterator = $classIterator->type($pattern);
     }
     foreach ($input->getOption('not-type') as $pattern) {
         $classIterator = $classIterator->not($classIterator->type($pattern));
     }
     foreach ($input->getOption('namespace') as $pattern) {
         $classIterator = $classIterator->inNamespace($pattern);
     }
     foreach ($input->getOption('not-namespace') as $pattern) {
         $classIterator = $classIterator->not($classIterator->inNamespace($pattern));
     }
     foreach ($input->getOption('def-name') as $pattern) {
         $classIterator = $classIterator->name($pattern);
     }
     foreach ($input->getOption('not-def-name') as $pattern) {
         $classIterator = $classIterator->not($classIterator->name($pattern));
     }
     if ($input->getOption('abstract')) {
         $classIterator = $classIterator->where('isAbstract');
     }
     if ($input->getOption('not-abstract')) {
         $classIterator = $classIterator->not($classIterator->where('isAbstract'));
     }
     if ($input->getOption('final')) {
         $classIterator = $classIterator->where('isFinal');
     }
     if ($input->getOption('not-final')) {
         $classIterator = $classIterator->not($classIterator->where('isFinal'));
     }
     if ($input->getOption('instantiable')) {
         $classIterator = $classIterator->where('isInstantiable');
     }
     if ($input->getOption('not-instantiable')) {
         $classIterator = $classIterator->not($classIterator->where('isInstantiable'));
     }
     if ($input->getOption('interface')) {
         $classIterator = $classIterator->where('isInterface');
     }
     if ($input->getOption('not-interface')) {
         $classIterator = $classIterator->not($classIterator->where('isInterface'));
     }
     if ($input->getOption('trait')) {
         $classIterator = $classIterator->where('isTrait');
     }
     if ($input->getOption('not-trait')) {
         $classIterator = $classIterator->not($classIterator->where('isTrait'));
     }
     if ($input->getOption('class')) {
         $classIterator = $classIterator->not($classIterator->where('isTrait'))->not($classIterator->where('isInterface'));
     }
     if ($input->getOption('not-class')) {
         $classIterator = $classIterator->not($classIterator->not($classIterator->where('isTrait'))->not($classIterator->where('isInterface')));
     }
     return $classIterator;
 }
Esempio n. 2
0
 /**
  * Load classmap at construct
  *
  * @param ClassIterator $classIterator
  * @param boolean       $register      True if loader should be register at creation
  */
 public function __construct(ClassIterator $classIterator, $register = true)
 {
     $this->classMap = $classIterator->getClassMap();
     if ($register) {
         $this->register();
     }
 }
 public function getAllCacheableClasses()
 {
     $finder = new Finder();
     $iterator = new ClassIterator($finder->in($this->directory));
     $iterator->enableAutoloading();
     $classes = [];
     foreach ($iterator->type(Model::class) as $className => $class) {
         if ($class->isInstantiable() && $this->usesCaching($class)) {
             $classes[] = $className;
         }
     }
     return $classes;
 }
Esempio n. 4
0
 /**
  * For every model found in the application and all modules checks if audit objects exist and are valid.
  * @param array $exclude as an array of model names
  * @return array
  */
 public function getReport($exclude = [])
 {
     $finder = new Finder();
     $iter = new ClassIterator($finder->in('.'));
     $iter->enableAutoloading();
     $result = $this->getBackendAuditManager()->checkGeneral();
     /** @var ReflectionClass $class */
     foreach ($iter->type('yii\\db\\ActiveRecord') as $name => $class) {
         if (in_array($name, $exclude)) {
             continue;
         }
         $result[$name] = $this->getBackendAuditManager()->checkModel(new $name());
     }
     return $result;
 }
 /**
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getExistingClasses(array $paths) : array
 {
     $namespaceList = [];
     $classList = [];
     $list = [];
     foreach ($paths as $path) {
         if (false === $path->isSourceCode()) {
             continue;
         }
         $iter = new ClassIterator($this->getPhpFiles($path->getPathBase()));
         foreach (array_keys($iter->getClassMap()) as $classname) {
             if (in_array(substr($classname, -4), ['Test', 'Spec'])) {
                 continue;
             }
             $reflection = new \ReflectionClass($classname);
             if ($reflection->isAbstract() || $reflection->isTrait() || $reflection->isInterface()) {
                 continue;
             }
             if ($reflection->inNamespace()) {
                 $namespaces = explode('\\', $reflection->getNamespaceName());
                 $namespace = '';
                 foreach ($namespaces as $key => $namespacePart) {
                     if ($namespace !== '') {
                         $namespace .= '/';
                     }
                     $namespace .= $namespacePart;
                     if (!array_key_exists($key, $namespaceList)) {
                         $namespaceList[$key] = [];
                     }
                     $namespaceList[$key][$namespace] = $namespace;
                 }
             }
             $escapedName = str_replace('\\', '/', $classname);
             $classList[$escapedName] = $escapedName;
         }
     }
     foreach ($namespaceList as $namespaceDepthList) {
         $list = array_merge($list, $namespaceDepthList);
     }
     $list = array_merge($list, $classList);
     return $list;
 }
Esempio n. 6
0
 protected function loadMediaModifiers()
 {
     if (!is_a($this->mediaRepository, Repository::class)) {
         throw new LetterpressException('Media repository was not initialized.');
     }
     $iter = new ClassIterator((new Finder())->in(__DIR__ . DIRECTORY_SEPARATOR . 'RichMedia'));
     $iter->enableAutoloading();
     $mediaModifiers = collect();
     foreach ($iter as $mediaModifier) {
         /** @var $mediaModifier \ReflectionClass */
         if ($mediaModifier->isSubclassOf(MediaModifier::class)) {
             $mediaModifiers[$mediaModifier->getShortName()] = $mediaModifier->getName();
         }
     }
     foreach (Config::get('letterpress.media.services') as $serviceName) {
         if ($mediaModifiers->has($serviceName)) {
             $class = $mediaModifiers->get($serviceName);
             $this->modifiers[] = new $class($this->mediaRepository);
         } else {
             throw new LetterpressException('Rich media integration for ' . $serviceName . ' is not available.');
         }
     }
 }
Esempio n. 7
0
 private function getModulesTablesMap()
 {
     if ($this->cacheDuration !== null && ($result = \Yii::$app->cache->get(self::CACHE_KEY)) !== false) {
         return $result;
     }
     $ds = DIRECTORY_SEPARATOR;
     $dirs = [\Yii::$app->getBasePath() . $ds . 'models'];
     foreach (\Yii::$app->getModules() as $moduleId => $module) {
         /** @var Module $module */
         if (!$module instanceof Module) {
             $module = \Yii::$app->getModule($moduleId);
         }
         if (file_exists($dir = $module->getBasePath() . $ds . 'models')) {
             $dirs[] = $dir;
         }
     }
     $iterator = new ClassIterator(Finder::create()->files()->name('*.php')->in($dirs));
     $iterator->enableAutoloading();
     $result = [];
     /** @var \ReflectionClass $class */
     foreach ($iterator->type('yii\\db\\ActiveRecord') as $name => $class) {
         /** @var \yii\db\ActiveRecord $name */
         $name = $class->name;
         $result[$name::getDb()->quoteSql($name::tableName())] = $name;
     }
     if ($this->cacheDuration !== null) {
         \Yii::$app->cache->set(self::CACHE_KEY, $result, $this->cacheDuration);
     }
     return $result;
 }
Esempio n. 8
0
 /**
  * Transforming classes
  *
  * Found class, interface and trait definitions can be transformed and
  * written to a single file.
  *
  * @expectOutputRegex /^\<\?php/
  */
 public function exampleMinimize()
 {
     $finder = new Finder();
     $iter = new ClassIterator($finder->in('src'));
     $iter->enableAutoloading();
     // Print all found definitions in one snippet
     echo $iter->minimize();
     // The same can be done using
     echo $iter->transform(new MinimizingWriter());
 }
Esempio n. 9
0
 /**
  * Simple enumerated output
  *
  * @param  OutputInterface $output
  * @param  ClassIterator   $classIterator
  * @return null
  */
 public function output(OutputInterface $output, ClassIterator $classIterator)
 {
     foreach ($classIterator->getClassMap() as $className => $file) {
         $output->writeln("{$file} => {$className}");
     }
 }