<?php
/**
 * Copyright 2017 OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require(__DIR__ . '/../vendor/autoload.php');

use Symfony\Component\Yaml\Yaml;

class ReleaseBuilder
{
    private $releases;
    private $template;

    public function __construct($yamlPath)
    {
        $loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
        $twig = new Twig_Environment($loader);
        $this->template = $twig->load('package.xml');
        $this->releases = Yaml::parse(file_get_contents($yamlPath))['releases'];
    }

    public function toXml()
    {
        $lastRelease = $this->releases[count($this->releases) - 1];

        $srcfiles = [
            'config.m4',
            'config.w32'
        ];
        $srcfiles = array_merge($srcfiles, array_map('basename', glob(__DIR__ . '/../ext/*.{c,h}', GLOB_BRACE)));

        // this file is generated by autoconf, skip it
        $srcfiles = array_diff($srcfiles, ['config.h']);

        $testfiles = [
            'common.php'
        ];
        $testfiles = array_merge($testfiles, array_map('basename', glob(__DIR__ . '/../ext/tests/*.phpt')));

        return $this->template->render([
            'releases' => $this->releases,
            'testfiles' => $testfiles,
            'srcfiles' => $srcfiles,
            'lastRelease' => $lastRelease
        ]);
    }
}

$releaseBuilder = new ReleaseBuilder(__DIR__ . '/../ext/releases.yaml');
echo $releaseBuilder->toXml();

?>
