RegexIterator example


function _get_block_templates_paths( $base_directory ) {
    $path_list = array();
    if ( file_exists( $base_directory ) ) {
        $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
        $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH );
        foreach ( $nested_html_files as $path => $file ) {
            $path_list[] = $path;
        }
    }
    return $path_list;
}

/** * Retrieves the template file from the theme for a given slug. * * @since 5.9.0 * @access private * * @param string $template_type 'wp_template' or 'wp_template_part'. * @param string $slug Template slug. * @return array|null { * Array with template metadata if $template_type is one of 'wp_template' or 'wp_template_part'. * null otherwise. * * @type string $slug Template slug. * @type string $path Template file path. * @type string $theme Theme slug. * @type string $type Template type. * @type string $area Template area. Only for 'wp_template_part'. * @type string $title Optional. Template title. * @type string[] $postTypes Optional. List of post types that the template supports. Only for 'wp_template'. * } */

        }

        return null;
    }

    private function getLatestMigrationVersion(string $migrationFolder): int
    {
        $regexPattern = '/^([0-9]*)-.+\.php$/i';

        $directoryIterator = new DirectoryIterator($migrationFolder);
        $regex = new RegexIterator($directoryIterator$regexPattern, RecursiveRegexIterator::GET_MATCH);
        $values = iterator_to_array($regex, false);

        if (empty($values)) {
            return 0;
        }

        $versions = array_column($values, '1');

        return empty($versions) ? 0 : (int) max($versions);
    }

    
if (!file_exists($templateDir)) {
            return;
        }

        $view->addTemplateDir($templateDir);
        $backendPath = $templateDir . 'backend/';
        if (!file_exists($backendPath)) {
            return;
        }

        $directoryIterator = new DirectoryIterator($backendPath);
        $regex = new RegexIterator($directoryIterator, '/^.+\.js$/i', RecursiveRegexIterator::GET_MATCH);
        foreach ($regex as $file) {
            $path = 'backend/' . $file[0];
            $view->extendsBlock(
                'backend/Emotion/app',
                PHP_EOL . '{include file="' . $path . '"}',
                'append'
            );
        }
    }

    public function registerWidgetTemplates(Enlight_Event_EventArgs $args)
    {
/** * Returns deprecations for templates contained in a directory. * * @param string $dir A directory where templates are stored * @param string $ext Limit the loaded templates by extension * * @return array An array of deprecations */
    public function collectDir(string $dir, string $ext = '.twig'): array
    {
        $iterator = new \RegexIterator(
            new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY
            ), '{'.preg_quote($ext).'$}'
        );

        return $this->collect(new TemplateDirIterator($iterator));
    }

    /** * Returns deprecations for passed templates. * * @param \Traversable $iterator An iterator of templates (where keys are template names and values the contents of the template) * * @return array An array of deprecations */
/** * Returns an array of all nested JSON files within a given directory. * * @since 6.2.0 * * @param string $dir The directory to recursively iterate and list files of. * @return array The merged array. */
    private static function recursively_iterate_json( $dir ) {
        $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
        $nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
        return $nested_json_files;
    }


    /** * Returns the style variations defined by the theme. * * @since 6.0.0 * @since 6.2.0 Returns parent theme variations if theme is a child. * * @return array */
// Rename hooks.     $theme_file = "$tmp_dir/$destination_theme.theme";
    if (file_exists($theme_file)) {
      if (!file_put_contents($theme_filepreg_replace("/(function )($source_theme_name)(_.*)/", "$1$destination_theme$3", file_get_contents($theme_file)))) {
        $io->getErrorStyle()->error("The theme file $theme_file could not be written.");
        return 1;
      }
    }

    // Rename references to libraries in templates.     $iterator = new TemplateDirIterator(new \RegexIterator(
      new \RecursiveIteratorIterator(
        new \RecursiveDirectoryIterator($tmp_dir), \RecursiveIteratorIterator::LEAVES_ONLY
      ), '/' . preg_quote('.html.twig') . '$/'
    ));

    foreach ($iterator as $template_file => $contents) {
      $new_template_content = preg_replace("/(attach_library\(['\")])$source_theme_name(\/.*['\"]\))/", "$1$destination_theme$2", $contents);
      if (!file_put_contents($template_file$new_template_content)) {
        $io->getErrorStyle()->error("The template file $template_file could not be written.");
        return 1;
      }
    }
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
    }

    /** * Load PHP files. */
    public function load()
    {
        foreach ($this->paths as $path) {
            $directory = new RecursiveDirectoryIterator($path['include']);
            $fullTree  = new RecursiveIteratorIterator($directory);
            $phpFiles  = new RegexIterator(
                $fullTree,
                '/.+((?<!Test)+\.php$)/i',
                RecursiveRegexIterator::GET_MATCH
            );

            foreach ($phpFiles as $key => $file) {
                foreach ($path['exclude'] as $exclude) {
                    if (str_contains($file[0]$exclude)) {
                        continue 2;
                    }
                }

                
parent::apply($migration$modus);
        }
    }

    public function getMigrationsForDowngrade($currentVersion$limit = null): array
    {
        $regexPattern = '/^([\d]*)-.+\.php$/i';

        $migrationPath = $this->getMigrationPath();

        $directoryIterator = new DirectoryIterator($migrationPath);
        $regex = new RegexIterator($directoryIterator$regexPattern, RecursiveRegexIterator::GET_MATCH);

        $migrations = [];

        foreach ($regex as $result) {
            $migrationVersion = $result['1'];

            if ($migrationVersion > $currentVersion) {
                continue;
            }

            $migrationClass = $this->loadMigration($result$migrationPath);

            
private function scanDirectoryForRegex($path$regex$regexFile = null)
    {
        // Iterate the given path recursively         $directoryIterator = new RecursiveDirectoryIterator($path);
        // get a flat iterator         $iterator = new RecursiveIteratorIterator($directoryIterator);

        $results = [];

        // Allow files to be filtered out by name         if (isset($regexFile) && !empty($regexFile)) {
            $iterator = new RegexIterator($iterator$regexFile);
        }

        // Iterate the result, get file content, check for $regex matches         foreach ($iterator as $splFileInfo) {
            if ($splFileInfo->isDir()) {
                continue;
            }

            $realPath = $splFileInfo->getRealPath();
            if (str_contains($realPath, 'SwagUpdateCheck')) {
                continue;
            }
if ($args->getSubject()->Request()->getModuleName() !== 'backend') {
            return;
        }

        $backendPath = $this->getExistingBackendEmotionPath();
        if (!\is_string($backendPath)) {
            return;
        }

        $directoryIterator = new DirectoryIterator($backendPath);
        $regex = new RegexIterator($directoryIterator, '/^.+\.js$/i', RecursiveRegexIterator::GET_MATCH);
        foreach ($regex as $file) {
            $path = 'backend/' . $file[0];
            $view->extendsBlock(
                'backend/Emotion/app',
                PHP_EOL . '{include file="' . $path . '"}',
                'append'
            );
        }
    }

    /** * Removes the snippets present in the plugin's ini files * from the database * * @param bool $removeDirty if true, the snippets changed by the * shop owner will also be removed */

    public function getMigrationsForVersion($currentVersion$limit = null)
    {
        $regexPattern = '/^([0-9]*)-.+\.php$/i';

        $migrationPath = $this->getMigrationPath();

        $directoryIterator = new DirectoryIterator($migrationPath);
        $regex = new RegexIterator($directoryIterator$regexPattern, RecursiveRegexIterator::GET_MATCH);

        $migrations = [];

        foreach ($regex as $result) {
            $migrationVersion = $result['1'];
            if ($migrationVersion <= $currentVersion) {
                continue;
            }

            $migrationClass = $this->loadMigration($result$migrationPath);

            
Home | Imprint | This part of the site doesn't use cookies.