<commit_before><?php

namespace Lfj\ZfRenderer\Service;

use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Stdlib\Response;
use Zend\View\HelperPluginManager;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
use Zend\View\Strategy\PhpRendererStrategy;
use Zend\View\View;

final class Renderer implements RendererInterface, EventManagerAwareInterface
{
    /**
     * @var HelperPluginManager
     */
    private $helperPluginManager;

    /**
     * @var EventManagerInterface
     */
    private $events;

    /**
     * @var array
     */
    private $resolvers;

    public function __construct()
    {
        $this->helperPluginManager = new HelperPluginManager();
        $this->resolvers = array();
    }

    /**
     * Render
     *
     * @inheritdoc
     */
    public function render($template, array $variables = array(), array $resolvers = array())
    {
        $view = $this->createView('view', $template, $resolvers);

        $model = new ViewModel();
        $model->setVariables($variables);
        $model->setTemplate('view');

        $view->render($model);

        return $view;
    }

    /**
     * @param $name
     * @param $path
     * @param $resolvers
     * @return View
     */
    private function createView($name, $path, $resolvers)
    {
        $ar = new Resolver\AggregateResolver();

        $helper = $this->helperPluginManager;

        $map = new Resolver\TemplateMapResolver(array(
            $name => $path
        ));

        $ar->attach($map);

        foreach ($resolvers as $r) {
            $ar->attach($r);
        }

        $renderer = new PhpRenderer();
        $renderer->setHelperPluginManager($helper);
        $renderer->setResolver($ar);

        $strategy = new PhpRendererStrategy($renderer);

        $view = new View();
        $view->setResponse(new Response());
        $view->getEventManager()->attach($strategy);

        return $view;
    }

    /**
     * @inheritdoc
     */
    public function setEventManager(EventManagerInterface $events)
    {
        $events->setIdentifiers(array(
            __CLASS__,
            get_class($this),
        ));

        $this->events = $events;
    }

    /**
     * @inheritdoc
     */
    public function getEventManager()
    {
        if (!$this->events instanceof EventManagerInterface) {
            $this->setEventManager(new EventManager());
        }

        return $this->events;
    }

    /**
     * @inheritdoc
     */
    public function setHelperPluginManager(HelperPluginManager $helperPluginManager)
    {
        $this->helperPluginManager = $helperPluginManager;
    }

    /**
     * @inheritdoc
     */
    public function getHelperPluginManager()
    {
        return $this->helperPluginManager;
    }
}
<commit_msg>Initialize $events in constructor instead of getter (lazy)<commit_after><?php

namespace Lfj\ZfRenderer\Service;

use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Stdlib\Response;
use Zend\View\HelperPluginManager;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
use Zend\View\Strategy\PhpRendererStrategy;
use Zend\View\View;

final class Renderer implements RendererInterface, EventManagerAwareInterface
{
    /**
     * @var HelperPluginManager
     */
    private $helperPluginManager;

    /**
     * @var EventManagerInterface
     */
    private $events;

    /**
     * @var array
     */
    private $resolvers;

    public function __construct()
    {
        $this->helperPluginManager = new HelperPluginManager();
        $this->events = new EventManager();
        $this->resolvers = array();
    }

    /**
     * Render
     *
     * @inheritdoc
     */
    public function render($template, array $variables = array(), array $resolvers = array())
    {
        $view = $this->createView('view', $template, $resolvers);

        $model = new ViewModel();
        $model->setVariables($variables);
        $model->setTemplate('view');

        $view->render($model);

        return $view;
    }

    /**
     * @param $name
     * @param $path
     * @param $resolvers
     * @return View
     */
    private function createView($name, $path, $resolvers)
    {
        $ar = new Resolver\AggregateResolver();

        $helper = $this->helperPluginManager;

        $map = new Resolver\TemplateMapResolver(array(
            $name => $path
        ));

        $ar->attach($map);

        foreach ($resolvers as $r) {
            $ar->attach($r);
        }

        $renderer = new PhpRenderer();
        $renderer->setHelperPluginManager($helper);
        $renderer->setResolver($ar);

        $strategy = new PhpRendererStrategy($renderer);

        $view = new View();
        $view->setResponse(new Response());
        $view->getEventManager()->attach($strategy);

        return $view;
    }

    /**
     * @inheritdoc
     */
    public function setEventManager(EventManagerInterface $events)
    {
        $events->setIdentifiers(array(
            __CLASS__,
            get_class($this),
        ));

        $this->events = $events;
    }

    /**
     * @inheritdoc
     */
    public function getEventManager()
    {
        return $this->events;
    }

    /**
     * @inheritdoc
     */
    public function setHelperPluginManager(HelperPluginManager $helperPluginManager)
    {
        $this->helperPluginManager = $helperPluginManager;
    }

    /**
     * @inheritdoc
     */
    public function getHelperPluginManager()
    {
        return $this->helperPluginManager;
    }
}

{'fraction_non_alphanumeric': 0.12842435875065433, 'fraction_numerical': 0.0, 'mean_word_length': 2.34032634032634, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2479195', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1621, 'n_words': 308}