<commit_before><?php
namespace Vivo\Backend;

use Vivo\Backend\ModuleResolver;
use Vivo\CMS\Api;
use Vivo\CMS\Security\Manager\AbstractManager;
use Vivo\IO\InputStreamInterface;
use Vivo\SiteManager\Event\SiteEvent;
use Vivo\UI\Component;
use Vivo\UI\ComponentTreeController;
use Vivo\Util\RedirectEvent;
use Vivo\Util\Redirector;
use Vivo\Util\UrlHelper;
use Vivo\UI\ComponentEventInterface;

use Zend\EventManager\EventInterface as Event;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\Stdlib\DispatchableInterface;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\View\Model\ModelInterface;

/**
 * The front controller which is responsible for dispatching all requests for documents and files in CMS repository.
 */
class BackendController implements DispatchableInterface,
    InjectApplicationEventInterface, ServiceManagerAwareInterface
{

    /**
     * @var MvcEvent
     */
    protected $mvcEvent;

    /**
     * @var SiteEvent
     */
    protected $siteEvent;

    /**
     * @var ComponentTreeController
     */
    protected $tree;

    /**
     *
     * @var Redirector
     */
    protected $redirector;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * Security manager.
     * @var AbstractManager
     */
    protected $securityManager;

    /**
     *
     * @var ModuleResolver
     */
    protected $moduleResolver;

    /**
     * @var UrlHelper
     */
    protected $urlHelper;

    /**
     * @var Api\Site
     */
    protected $siteApi;

    /**
     * Constructor.
     * @param AbstractManager $securityManager
     */
    public function __construct(AbstractManager $securityManager)
    {
        $this->securityManager = $securityManager;
    }

    /**
     * @param SiteEvent $site
     */
    public function setSiteEvent(SiteEvent $siteEvent)
    {
        $this->siteEvent = $siteEvent;
    }

    /**
     * Dispatches backend request
     * @param Request $request
     * @param Response $response
     * @todo should we render UI in controller dispatch action?
     */
    public function dispatch(Request $request, Response $response = null)
    {

        $sm = $this->sm;

        //redirect bad backend urls
        $host = $this->mvcEvent->getRouteMatch()->getParam('host');
        if ($this->securityManager->getUserPrincipal()) {
            if ($this->mvcEvent->getRouteMatch()->getMatchedRouteName() == 'backend/other'
                || $this->mvcEvent->getRouteMatch()->getMatchedRouteName() == 'backend/default'
                || !$this->mvcEvent->getRouteMatch()->getParam('module')
                || !$host) {
                $url = $this->urlHelper->fromRoute('backend/modules/query', array('host' => $host?:$this->getDefaultHost()));
                $this->redirector->redirect(new RedirectEvent($url));
                return $response;
            }
        }

        //Create UI component tree for backend.
        $root = $sm->get('Vivo\CMS\UI\Root');
        $page = $sm->get('Vivo\UI\Page');
        if (!$this->securityManager->getUserPrincipal()) {
            $page->setMain($this->sm->get('Vivo\Backend\UI\Logon'));
        } else {
            $backend = $this->sm->get('Vivo\Backend\UI\Backend');
            $moduleName = $this->mvcEvent->getRouteMatch()->getParam('module');
            $backend->setModuleComponent($this->moduleResolver->createComponent($moduleName));
            $page->setMain($backend);
        }
        $root->setMain($page);

        // Perform tree operations
        $this->tree->setRoot($root);
        $this->tree->loadState();

        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->tree->init(); //replace by lazy init<commit_msg>Route name in BackendController fixed<commit_after><?php
namespace Vivo\Backend;

use Vivo\Backend\ModuleResolver;
use Vivo\CMS\Api;
use Vivo\CMS\Security\Manager\AbstractManager;
use Vivo\IO\InputStreamInterface;
use Vivo\SiteManager\Event\SiteEvent;
use Vivo\UI\Component;
use Vivo\UI\ComponentTreeController;
use Vivo\Util\RedirectEvent;
use Vivo\Util\Redirector;
use Vivo\Util\UrlHelper;
use Vivo\UI\ComponentEventInterface;

use Zend\EventManager\EventInterface as Event;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\Stdlib\DispatchableInterface;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\View\Model\ModelInterface;

/**
 * The front controller which is responsible for dispatching all requests for documents and files in CMS repository.
 */
class BackendController implements DispatchableInterface,
    InjectApplicationEventInterface, ServiceManagerAwareInterface
{

    /**
     * @var MvcEvent
     */
    protected $mvcEvent;

    /**
     * @var SiteEvent
     */
    protected $siteEvent;

    /**
     * @var ComponentTreeController
     */
    protected $tree;

    /**
     *
     * @var Redirector
     */
    protected $redirector;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * Security manager.
     * @var AbstractManager
     */
    protected $securityManager;

    /**
     *
     * @var ModuleResolver
     */
    protected $moduleResolver;

    /**
     * @var UrlHelper
     */
    protected $urlHelper;

    /**
     * @var Api\Site
     */
    protected $siteApi;

    /**
     * Constructor.
     * @param AbstractManager $securityManager
     */
    public function __construct(AbstractManager $securityManager)
    {
        $this->securityManager = $securityManager;
    }

    /**
     * @param SiteEvent $site
     */
    public function setSiteEvent(SiteEvent $siteEvent)
    {
        $this->siteEvent = $siteEvent;
    }

    /**
     * Dispatches backend request
     * @param Request $request
     * @param Response $response
     * @todo should we render UI in controller dispatch action?
     */
    public function dispatch(Request $request, Response $response = null)
    {

        $sm = $this->sm;

        //redirect bad backend urls
        $host = $this->mvcEvent->getRouteMatch()->getParam('host');
        if ($this->securityManager->getUserPrincipal()) {
            if ($this->mvcEvent->getRouteMatch()->getMatchedRouteName() == 'backend/other'
                || $this->mvcEvent->getRouteMatch()->getMatchedRouteName() == 'backend/default'
                || !$this->mvcEvent->getRouteMatch()->getParam('module')
                || !$host) {
                $url = $this->urlHelper->fromRoute('backend/modules', array('host' => $host?:$this->getDefaultHost()));
                $this->redirector->redirect(new RedirectEvent($url));
                return $response;
            }
        }

        //Create UI component tree for backend.
        $root = $sm->get('Vivo\CMS\UI\Root');
        $page = $sm->get('Vivo\UI\Page');
        if (!$this->securityManager->getUserPrincipal()) {
            $page->setMain($this->sm->get('Vivo\Backend\UI\Logon'));
        } else {
            $backend = $this->sm->get('Vivo\Backend\UI\Backend');
            $moduleName = $this->mvcEvent->getRouteMatch()->getParam('module');
            $backend->setModuleComponent($this->moduleResolver->createComponent($moduleName));
            $page->setMain($backend);
        }
        $root->setMain($page);

        // Perform tree operations
        $this->tree->setRoot($root);
        $this->tree->loadState();

        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->tree->init(); //replace by lazy init
{'fraction_non_alphanumeric': 0.13441409241766658, 'fraction_numerical': 0.0, 'mean_word_length': 2.647579143389199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '7472217', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2204, 'n_words': 431}