<?php
/**
 * Pi Engine (http://piengine.org)
 *
 * @link            http://code.piengine.org for the Pi Engine source repository
 * @copyright       Copyright (c) Pi Engine http://piengine.org
 * @license         http://piengine.org/license.txt BSD 3-Clause License
 * @package         View
 */

namespace Pi\View\Helper\Navigation;

use Pi;
use Laminas\Navigation\AbstractContainer;
use Laminas\View\Helper\Navigation\Sitemap as LaminasSitemap;

/**
 * Navigation sitemap helper
 *
 * {@inheritDoc}
 * @author Taiwen Jiang <taiwenjiang@tsinghua.org.cn>
 */
class Sitemap extends LaminasSitemap
{
    /**
     * Cache container
     *
     * @var \StdClass
     */
    protected $cache;

    /**
     * Set cache container
     *
     * @param \StdClass $cache
     */
    public function setCache($cache)
    {
        $this->cache = $cache;
    }

    /**
     * Renders helper
     *
     * @param AbstractContainer $container
     *      [optional] container to render.
     *      Default is to render the container registered in the helper.
     *
     * @return string
     */
    public function render($container = null)
    {
        Pi::service('log')->start('sitemap');

        // Try to load from cache
        if ($this->cache) {
            $cacheKey = $this->cache->key . '-sp';
            $content  = $this->cache->storage->getItem($cacheKey);
            if (null !== $content) {
                if (Pi::service()->hasService('log')) {
                    Pi::service('log')->info('Sitemap is loaded from cache.');
                }
                return $content;
            }
        }

        // Generate if no cache available
        $content = parent::render($container);

        // Save to cache
        if ($this->cache) {
            $this->cache->storage->setItem($cacheKey, $content);
        }

        Pi::service('log')->end('sitemap');

        return $content;
    }
}
