Graby\SiteConfig\ConfigBuilder::parseLines PHP Method

parseLines() public method

Parse line from the config file to build the config.
public parseLines ( array $lines ) : SiteConfig
$lines array
return SiteConfig
    public function parseLines(array $lines)
    {
        $config = new SiteConfig();
        foreach ($lines as $line) {
            $line = trim($line);
            // skip comments, empty lines
            if ($line == '' || $line[0] == '#') {
                continue;
            }
            // get command
            $command = explode(':', $line, 2);
            // if there's no colon ':', skip this line
            if (count($command) != 2) {
                continue;
            }
            $val = trim($command[1]);
            $command = trim($command[0]);
            if ($command == '' || $val == '') {
                continue;
            }
            // check for commands where we accept multiple statements
            if (in_array($command, array('title', 'body', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'next_page_link', 'test_url', 'find_string', 'replace_string', 'login_extra_fields'))) {
                array_push($config->{$command}, $val);
                // check for single statement commands that evaluate to true or false
            } elseif (in_array($command, array('tidy', 'prune', 'autodetect_on_failure', 'requires_login'))) {
                $config->{$command} = $val == 'yes' || $val == 'true';
                // check for single statement commands stored as strings
            } elseif (in_array($command, array('parser', 'login_username_field', 'login_password_field', 'not_logged_in_xpath', 'login_uri'))) {
                $config->{$command} = $val;
                // check for replace_string(find): replace
            } elseif (substr($command, -1) == ')' && preg_match('!^([a-z0-9_]+)\\((.*?)\\)$!i', $command, $match) && $match[1] == 'replace_string') {
                array_push($config->find_string, $match[2]);
                array_push($config->replace_string, $val);
            } elseif (substr($command, -1) == ')' && preg_match('!^([a-z0-9_]+)\\(([a-z0-9_-]+)\\)$!i', $command, $match) && $match[1] == 'http_header' && in_array($match[2], array('user-agent', 'referer'))) {
                $config->http_header[$match[2]] = $val;
            }
        }
        return $config;
    }

Usage Example

コード例 #1
0
ファイル: ConfigBuilderTest.php プロジェクト: harikt/graby
 public function testBuildFromArray()
 {
     $configBuilder = new ConfigBuilder(array('site_config' => array(dirname(__FILE__))));
     $configActual = $configBuilder->parseLines(array('# this is a comment and it will be removed', 'no colon on this line, it will be removed', '   : empty value before colon, it will be removed', 'title: hoho', 'tidy: yes', 'parser: bob', 'replace_string(toto): titi'));
     $configExpected = new SiteConfig();
     $configExpected->title = array('hoho');
     $configExpected->tidy = true;
     $configExpected->parser = 'bob';
     $configExpected->find_string = array('toto');
     $configExpected->replace_string = array('titi');
     $this->assertEquals($configExpected, $configActual);
     // without using default value
     $this->assertTrue($configActual->tidy(false));
     $this->assertEquals('bob', $configActual->parser(false));
     $this->assertNull($configActual->prune(false));
     $this->assertNull($configActual->autodetect_on_failure(false));
     // using default values
     $this->assertTrue($configActual->tidy(true));
     $this->assertEquals('bob', $configActual->parser(true));
     $this->assertTrue($configActual->prune(true));
     $this->assertTrue($configActual->autodetect_on_failure(true));
 }