Joomlatools\Console\Command\Site\Listing::execute PHP Method

execute() protected method

protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        define('_JEXEC', true);
        define('JPATH_BASE', true);
        define('JPATH_PLATFORM', true);
        $docroot = $input->getOption('www');
        if (!file_exists($docroot)) {
            throw new \RuntimeException(sprintf('Web server root \'%s\' does not exist.', $docroot));
        }
        $dir = new \DirectoryIterator($docroot);
        $sites = array();
        $canonical = function ($version) {
            if (isset($version->RELEASE)) {
                return 'v' . $version->RELEASE . '.' . $version->DEV_LEVEL;
            }
            // Joomla 3.5 and up uses constants instead of properties in JVersion
            $className = get_class($version);
            if (defined("{$className}::RELEASE")) {
                return 'v' . $version::RELEASE . '.' . $version::DEV_LEVEL;
            }
            return 'unknown';
        };
        foreach ($dir as $fileinfo) {
            $code = $application = null;
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                $files = array('joomla-cms' => $fileinfo->getPathname() . '/libraries/cms/version/version.php', 'joomlatools-platform' => $fileinfo->getPathname() . '/lib/libraries/cms/version/version.php', 'joomla-1.5' => $fileinfo->getPathname() . '/libraries/joomla/version.php');
                foreach ($files as $type => $file) {
                    if (file_exists($file)) {
                        $code = $file;
                        $application = $type;
                        break;
                    }
                }
                if (!is_null($code) && file_exists($code)) {
                    $identifier = uniqid();
                    $source = file_get_contents($code);
                    $source = preg_replace('/<\\?php/', '', $source, 1);
                    $source = preg_replace('/class JVersion/i', 'class JVersion' . $identifier, $source);
                    eval($source);
                    $class = 'JVersion' . $identifier;
                    $version = new $class();
                    $sites[] = (object) array('name' => $fileinfo->getFilename(), 'docroot' => $docroot . '/' . $fileinfo->getFilename() . '/' . ($application == 'joomlatools-platform' ? 'web' : ''), 'type' => $application, 'version' => $canonical($version));
                }
            }
        }
        if (!in_array($input->getOption('format'), array('txt', 'json'))) {
            throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $input->getOption('format')));
        }
        switch ($input->getOption('format')) {
            case 'json':
                $result = new \stdClass();
                $result->command = $input->getArgument('command');
                $result->data = $sites;
                $options = version_compare(phpversion(), '5.4.0') >= 0 ? JSON_PRETTY_PRINT : 0;
                $string = json_encode($result, $options);
                break;
            case 'txt':
            default:
                $lines = array();
                foreach ($sites as $i => $site) {
                    $lines[] = sprintf("<info>%s. %s</info> (%s %s)", $i + 1, $site->name, $site->type, $site->version);
                }
                $string = implode("\n", $lines);
                break;
        }
        $output->writeln($string);
    }