Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. System.php

function System::find

The "find" command

Usage:

System::find($dir); System::find("$dir -type d"); System::find("$dir -type f"); System::find("$dir -name *.php"); System::find("$dir -name *.php -name *.htm*"); System::find("$dir -maxdepth 1");

Params implemented: $dir -> Start the search at this directory -type d -> return only directories -type f -> return only files -maxdepth <n> -> max depth of recursion -name <pattern> -> search pattern (bash style). Multiple -name param allowed

Parameters

mixed Either array or string with the command line:

Return value

array Array of found files

File

vendor/pear/pear-core-minimal/src/System.php, line 568

Class

System
System offers cross platform compatible system functions

Code

public static function find($args) {
    if (!is_array($args)) {
        $args = preg_split('/\\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
    }
    $dir = realpath(array_shift($args));
    if (!$dir) {
        return array();
    }
    $patterns = array();
    $depth = 0;
    $do_files = $do_dirs = true;
    $args_count = count($args);
    for ($i = 0; $i < $args_count; $i++) {
        switch ($args[$i]) {
            case '-type':
                if (in_array($args[$i + 1], array(
                    'd',
                    'f',
                ))) {
                    if ($args[$i + 1] == 'd') {
                        $do_files = false;
                    }
                    else {
                        $do_dirs = false;
                    }
                }
                $i++;
                break;
            case '-name':
                $name = preg_quote($args[$i + 1], '#');
                // our magic characters ? and * have just been escaped,
                // so now we change the escaped versions to PCRE operators
                $name = strtr($name, array(
                    '\\?' => '.',
                    '\\*' => '.*',
                ));
                $patterns[] = '(' . $name . ')';
                $i++;
                break;
            case '-maxdepth':
                $depth = $args[$i + 1];
                break;
        }
    }
    $path = System::_dirToStruct($dir, $depth, 0, true);
    if ($do_files && $do_dirs) {
        $files = array_merge($path['files'], $path['dirs']);
    }
    elseif ($do_dirs) {
        $files = $path['dirs'];
    }
    else {
        $files = $path['files'];
    }
    if (count($patterns)) {
        $dsq = preg_quote(DIRECTORY_SEPARATOR, '#');
        $pattern = '#(^|' . $dsq . ')' . implode('|', $patterns) . '($|' . $dsq . ')#';
        $ret = array();
        $files_count = count($files);
        for ($i = 0; $i < $files_count; $i++) {
            // only search in the part of the file below the current directory
            $filepart = basename($files[$i]);
            if (preg_match($pattern, $filepart)) {
                $ret[] = $files[$i];
            }
        }
        return $ret;
    }
    return $files;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal