/** * @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; }
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; }
/** * 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; }
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.'); } } }
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; }
/** * 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()); }