Пример #1
0
 /**
  * Returns a formatted string consisting of all
  * messages from the results of the given report.
  *
  * @param IReport $report report to take results (and messages) from
  *
  * @return string messages formatted with the configured format
  */
 public function format(IReport $report)
 {
     $output = '';
     $format = $this->getParameters()->get('format', self::DEFAULT_FORMAT);
     $results = $report->getResults();
     foreach ($results as $result) {
         $messages = $result->getMessages();
         foreach ($messages as $message) {
             switch ($message->getSeverity()) {
                 case Message::SEVERITY_FATAL:
                 case Message::SEVERITY_ERROR:
                     $output .= sprintf($format, $message->getGroup(), $message->getName(), '<error>' . $message->getText() . '</error>');
                     break;
                 case Message::SEVERITY_NOTICE:
                 case Message::SEVERITY_WARN:
                     $output .= sprintf($format, $message->getGroup(), $message->getName(), '<comment>' . $message->getText() . '</comment>');
                     break;
                 case Message::SEVERITY_INFO:
                     $output .= sprintf($format, $message->getGroup(), $message->getName(), '<info>' . $message->getText() . '</info>');
                     break;
                 case Message::SEVERITY_DEBUG:
                 default:
                     $output .= sprintf($format, $message->getGroup(), $message->getName(), $message->getText());
                     break;
             }
             $output .= PHP_EOL;
         }
     }
     return $output;
 }
Пример #2
0
 /**
  * Write the cachable settings from the run checks to a cache file for later reuse.
  *
  * @todo should cache only be written if all checks succeeded?
  *
  * @param boolean $run_was_successful whether or not the checks ran successfully
  */
 protected function writeCache($run_was_successful)
 {
     $disable_caching = $this->input->getOption('no-cache');
     if (!empty($disable_caching)) {
         return;
     }
     $cache_implementor = $this->config->getCacheImplementor();
     $cache = new $cache_implementor();
     if (!$cache instanceof ICache) {
         throw new \InvalidArgumentException('The given cache class "' . $cache_implementor . '" does not implement ICache.');
     }
     $cache_params = new Parameters($this->config->get('cache', array()));
     $cache->setParameters($cache_params);
     // TODO use something like getenv('ENVIRONAUT_CACHE_DIR')?
     $cache_location = $this->input->getOption('cache-location');
     if (!empty($cache_location)) {
         $cache->setLocation($cache_location);
     }
     $cache->addAll($this->report->getCachableSettings());
     if ($cache->save()) {
         $this->output->writeln(PHP_EOL . 'Writing cachable settings to "<comment>' . $cache->getLocation() . '</comment>" for subsequent runs...<info>ok</info>.' . PHP_EOL);
     } else {
         $this->output->writeln(PHP_EOL . 'Writing cachable settings to "<comment>' . $cache->getLocation() . '</comment>" for subsequent runs...<error>FAILED</error>.' . PHP_EOL);
     }
 }
Пример #3
0
 /**
  * Writes all or specific groups of settings as a JSON file and
  * returns a message with some information about that.
  *
  * @param IReport $report report to take results (and settings) from
  *
  * @return string messages for CLI output
  */
 public function format(IReport $report)
 {
     $output = '';
     $params = $this->getParameters();
     $file = $params->get('location', 'environaut-config.json');
     $pretty = $params->get('pretty', true);
     $nested = $params->get('nested', true);
     $groups = $params->get('groups');
     if (is_writable($file)) {
         $output .= '<comment>Overwriting</comment> ';
     } else {
         $output .= 'Writing ';
     }
     if (empty($groups)) {
         $output .= 'all groups ';
     } else {
         $output .= 'group(s) "' . implode(', ', $groups) . '" ';
     }
     $output .= 'to file "<comment>' . $file . '</comment>"...';
     $flags = JSON_FORCE_OBJECT;
     if ($pretty && version_compare(PHP_VERSION, '5.4.0') >= 0) {
         $flags |= JSON_PRETTY_PRINT;
     }
     $all_settings = $report->getSettingsAsArray($groups);
     $grouped_settings = array();
     foreach ($all_settings as $setting) {
         if ($nested === true) {
             $grouped_settings[$setting['group']][$setting['name']] = $setting['value'];
         } else {
             $grouped_settings[$setting['name']] = $setting['value'];
         }
     }
     $content = json_encode($grouped_settings, $flags);
     $ok = file_put_contents($file, $content, LOCK_EX);
     if ($ok !== false) {
         $output .= '<info>ok</info>.';
     } else {
         $output .= '<error>FAILED</error>.';
     }
     return $output;
 }
Пример #4
0
    /**
     * Writes all or specific groups of settings as a PHP file and
     * returns a message with some information about that.
     *
     * @param IReport $report report to take results (and settings) from
     *
     * @return string messages for CLI output
     */
    public function format(IReport $report)
    {
        $output = '';
        $params = $this->getParameters();
        $file = $params->get('location', 'environaut-config.php');
        $groups = $params->get('groups');
        $nested = $params->get('nested', true);
        if (is_writable($file)) {
            $output .= '<comment>Overwriting</comment> ';
        } else {
            $output .= 'Writing ';
        }
        if (empty($groups)) {
            $output .= 'all groups ';
        } else {
            $output .= 'group(s) "' . implode(', ', $groups) . '" ';
        }
        $output .= 'to file "<comment>' . $file . '</comment>"...';
        $default_template = <<<EOT
<?php return %settings\$s;
EOT;
        $template = $params->get('template', $default_template);
        $all_settings = $report->getSettingsAsArray($groups);
        $grouped_settings = array();
        foreach ($all_settings as $setting) {
            if ($nested === true) {
                $grouped_settings[$setting['group']][$setting['name']] = $setting['value'];
            } else {
                $grouped_settings[$setting['name']] = $setting['value'];
            }
        }
        $content = XmlSettingsWriter::vksprintf($template, array('settings' => var_export($grouped_settings, true)));
        $ok = file_put_contents($file, $content, LOCK_EX);
        if ($ok !== false) {
            $output .= '<info>ok</info>.';
        } else {
            $output .= '<error>FAILED</error>.';
        }
        return $output;
    }
Пример #5
0
    /**
     * Writes all or specific groups of settings as a shell file and
     * returns a message with some information about that.
     *
     * @param IReport $report report to take results (and settings) from
     *
     * @return string messages for CLI output
     */
    public function format(IReport $report)
    {
        $output = '';
        $params = $this->getParameters();
        $file = $params->get('location', 'environaut-config.sh');
        $groups = $params->get('groups');
        if (is_writable($file)) {
            $output .= '<comment>Overwriting</comment> ';
        } else {
            $output .= 'Writing ';
        }
        if (empty($groups)) {
            $output .= 'all groups ';
        } else {
            $output .= 'group(s) "' . implode(', ', $groups) . '" ';
        }
        $output .= 'to file "<comment>' . $file . '</comment>"...';
        $default_template = <<<EOT
%settings\$s
EOT;
        $template = $params->get('template', $default_template);
        $all_settings = $report->getSettingsAsArray($groups);
        $grouped_settings = array();
        $content = '';
        foreach ($all_settings as $setting) {
            $name = $this->makeShellVariableName($setting['name'], $setting['group']);
            $value = $this->mapValue($setting['value']);
            $content .= $name . "='" . $value . "'\n";
        }
        $content = self::vksprintf($template, array('settings' => $content));
        $ok = file_put_contents($file, $content, LOCK_EX);
        if ($ok !== false) {
            $output .= '<info>ok</info>.';
        } else {
            $output .= '<error>FAILED</error>.';
        }
        return $output;
    }
Пример #6
0
 /**
  * Writes all or specific groups of settings as a plain text file and
  * returns a message with some information about that.
  *
  * @param IReport $report report to take results (and settings) from
  *
  * @return string messages for CLI output
  */
 public function format(IReport $report)
 {
     $params = $this->getParameters();
     $file = $params->get('location', 'environaut-config');
     $groups = $params->get('groups');
     $output = $this->startOutput($file, $groups);
     $embed_group_path = $params->get('embed_group_path', true);
     $filter_settings = $params->get('filter_settings', array());
     $template = $params->get('template', false);
     if (!$template) {
         throw new RuntimeException(sprintf("The %s does not support a default template. Please define one.", __CLASS__));
     }
     $template_settings = array();
     foreach ($report->getSettingsAsArray($groups) as $setting) {
         if ($embed_group_path === true) {
             $setting_key = $setting['group'] . '.' . $setting['name'];
         } else {
             $setting_key = $setting['name'];
         }
         $template_settings[$setting_key] = $setting['value'];
     }
     $content = self::vksprintf($template, $template_settings);
     return $this->endOutput(file_put_contents($file, $content, LOCK_EX));
 }
Пример #7
0
    /**
     * Writes all or specific groups of settings as a JSON file and
     * returns a message with some information about that.
     *
     * @param IReport $report report to take results (and settings) from
     *
     * @return string messages for CLI output
     */
    public function format(IReport $report)
    {
        $output = '';
        $params = $this->getParameters();
        $file = $params->get('location', 'environaut-config.xml');
        $groups = $params->get('groups');
        if (is_writable($file)) {
            $output .= '<comment>Overwriting</comment> ';
        } else {
            $output .= 'Writing ';
        }
        if (empty($groups)) {
            $output .= 'all groups ';
        } else {
            $output .= 'group(s) "' . implode(', ', $groups) . '" ';
        }
        $default_file_template = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations
    xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0"
    xmlns:xi="http://www.w3.org/2001/XInclude"
    xmlns="http://agavi.org/agavi/config/parts/settings/1.0"
>
    <ae:configuration>
%group_template\$s
    </ae:configuration>
</ae:configurations>

EOT;
        $default_group_template = <<<EOT

        <settings prefix="%group_name\$s.">%setting_template\$s
        </settings>

EOT;
        $default_setting_template = <<<EOT

            <setting name="%setting_name\$s">%setting_value\$s</setting>
EOT;
        $file_template = $params->get('file_template', $default_file_template);
        $group_template = $params->get('group_template', $default_group_template);
        $setting_template = $params->get('setting_template', $default_setting_template);
        $all_settings = $report->getSettings($groups);
        $grouped_settings = array();
        foreach ($all_settings as $setting) {
            $grouped_settings[$setting->getGroup()][] = $setting;
        }
        $group_content = '';
        foreach ($grouped_settings as $group_name => $settings) {
            $settings_content = '';
            // remove control characters like vertical tabs, up/down arrows etc. as it breaks sprintf templates -.-
            $group_name = preg_replace('/[[:cntrl:]]/', '', $group_name);
            foreach ($settings as $setting) {
                $value = $setting->getValue();
                if (is_bool($value)) {
                    $value = var_export($value, true);
                    // we want "true"/"false" instead of "1"/"" in the output
                }
                // remove control characters like vertical tabs, up/down arrows etc.
                // this leads arrow-up key "\033[A" being converted to "[A" which is probably not useful,
                // but at least does not break the cheapo sprintf templating for the moment...
                $name = preg_replace('/[[:cntrl:]]/', '', $setting->getName());
                $value = preg_replace('/[[:cntrl:]]/', '', $value);
                //$value = preg_replace('/[^\p{L}\s]/u','',$value);
                //$value = preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $value);
                $settings_content .= self::vksprintf($setting_template, array('setting_name' => htmlspecialchars($name, ENT_QUOTES, 'UTF-8'), 'setting_value' => htmlspecialchars($value, ENT_QUOTES, 'UTF-8'), 'group_name' => htmlspecialchars($group_name, ENT_QUOTES, 'UTF-8')));
            }
            $group_content .= self::vksprintf($group_template, array('setting_template' => $settings_content, 'group_name' => htmlspecialchars($group_name, ENT_QUOTES, 'UTF-8')));
        }
        $content = self::vksprintf($file_template, array('group_template' => $group_content));
        $output .= 'to file "<comment>' . $file . '</comment>"...';
        $ok = file_put_contents($file, $content, LOCK_EX);
        if ($ok !== false) {
            $output .= '<info>ok</info>.';
        } else {
            $output .= '<error>FAILED</error>.';
        }
        return $output;
    }