Potsky\LaravelLocalizationHelpers\Factory\Localization::findLemma PHP Метод

findLemma() публичный Метод

Get the list of PHP code files where a lemma is defined
public findLemma ( string $lemma, array $folders, array $trans_methods, boolean | false $regex = false, boolean | false $shortOutput = false, string $ext = 'php' ) : array | false
$lemma string A lemma to search for or a regex to search for
$folders array An array of folder to search for lemma in
$trans_methods array An array of PHP lang functions
$regex boolean | false Is lemma a regex ?
$shortOutput boolean | false Output style for file paths
$ext string
Результат array | false
    public function findLemma($lemma, $folders, $trans_methods, $regex = false, $shortOutput = false, $ext = 'php')
    {
        $files = array();
        foreach ($folders as $path) {
            foreach ($this->getFilesWithExtension($path, $ext) as $php_file_path => $dumb) {
                foreach ($this->extractTranslationFromPhpFile($php_file_path, $trans_methods) as $k => $v) {
                    $real_value = eval("return {$k};");
                    $found = false;
                    if ($regex) {
                        try {
                            $r = preg_match($lemma, $real_value);
                        } catch (\Exception $e) {
                            $this->messageBag->writeError("The argument is not a valid regular expression:" . str_replace('preg_match():', '', $e->getMessage()));
                            return false;
                        }
                        if ($r === 1) {
                            $found = true;
                        } else {
                            if ($r === false) {
                                $this->messageBag->writeError("The argument is not a valid regular expression");
                                return false;
                            }
                        }
                        // @codeCoverageIgnoreEnd
                    } else {
                        if (!(strpos($real_value, $lemma) === false)) {
                            $found = true;
                        }
                    }
                    if ($found === true) {
                        if ($shortOutput === true) {
                            $php_file_path = $this->getShortPath($php_file_path);
                        }
                        $files[] = $php_file_path;
                        break;
                    }
                }
            }
        }
        return $files;
    }

Usage Example

 /**
  *
  */
 public function testSearchForRegexLemma()
 {
     /** @noinspection PhpVoidFunctionResultUsedInspection */
     $return = Artisan::call('localization:find', array('lemma' => 'message\\.lemma.*', '--verbose' => true, '--short' => true, '--regex' => true));
     $this->assertEquals(1, $return);
     $this->assertContains('The argument is not a valid regular expression:', Artisan::output());
     /** @noinspection PhpVoidFunctionResultUsedInspection */
     $return = Artisan::call('localization:find', array('lemma' => '@message\\.lemma.*@', '--verbose' => true, '--short' => true, '--regex' => true));
     $this->assertEquals(0, $return);
     $this->assertContains('has been found in', Artisan::output());
     $messageBag = new MessageBag();
     $manager = new Localization($messageBag);
     $trans_methods = array('trans' => array('@trans\\(\\s*(\'.*\')\\s*(,.*)*\\)@U', '@trans\\(\\s*(".*")\\s*(,.*)*\\)@U'), 'Lang::Get' => array('@Lang::Get\\(\\s*(\'.*\')\\s*(,.*)*\\)@U', '@Lang::Get\\(\\s*(".*")\\s*(,.*)*\\)@U', '@Lang::get\\(\\s*(\'.*\')\\s*(,.*)*\\)@U', '@Lang::get\\(\\s*(".*")\\s*(,.*)*\\)@U'), 'trans_choice' => array('@trans_choice\\(\\s*(\'.*\')\\s*,.*\\)@U', '@trans_choice\\(\\s*(".*")\\s*,.*\\)@U'), 'Lang::choice' => array('@Lang::choice\\(\\s*(\'.*\')\\s*,.*\\)@U', '@Lang::choice\\(\\s*(".*")\\s*,.*\\)@U'), '@lang' => array('@\\@lang\\(\\s*(\'.*\')\\s*(,.*)*\\)@U', '@\\@lang\\(\\s*(".*")\\s*(,.*)*\\)@U'), '@choice' => array('@\\@choice\\(\\s*(\'.*\')\\s*,.*\\)@U', '@\\@choice\\(\\s*(".*")\\s*,.*\\)@U'));
     $return = $manager->findLemma('not a valid regex', $manager->getPath(self::MOCK_DIR_PATH_GLOBAL), $trans_methods, true, true);
     $messages = $messageBag->getMessages();
     $this->assertFalse($return);
     $this->assertContains('The argument is not a valid regular expression:', $messages[0][1]);
 }