<commit_before><?php declare(strict_types=1);
/**
 * Uthando CMS (http://www.shaunfreeman.co.uk/)
 *
 * @author      <NAME> <<EMAIL>>
 * @link        https://github.com/uthando-cms for the canonical source repository
 * @copyright   Copyright (c) 24/09/17 <NAME>. (http://www.shaunfreeman.co.uk)
 * @license     see LICENSE
 */

namespace UthandoThemeManager\View;

use UthandoCommon\Service\ServiceManager;
use UthandoCommon\View\AbstractViewHelper;
use UthandoThemeManager\Model\WidgetModel;
use UthandoThemeManager\Service\WidgetGroupManager;
use UthandoThemeManager\Service\WidgetManager;

class WidgetHelper extends AbstractViewHelper
{
    /**
     * @var WidgetGroupManager
     */
    protected $widgetGroupManager;

    /**
     * @var WidgetManager
     */
    protected $widgetManager;

    public function __invoke(string $name): string
    {
        $widgetOrGroup = strstr($name, '-', true);
        $widgetName = substr(strstr($name, '-'), 1);

        if ('group' === $widgetOrGroup) {
            $html = $this->getWidgetGroup($widgetName);
        } else {
            $html = $this->getWidget($name);
        }

        return $html;
    }

    public function getWidgetGroup(string $group): string
    {
        $widgetGroup = $this->getWidgetGroupService()->getWidgetGroupByName($group);

        if (!$widgetGroup) return '';

        $params         = $widgetGroup->parseParams();
        $class          = $params['class'] ?? '';
        $html           = ($class) ? '<div class="' . $this->getView()->escapeHtml($class) . '">' : '';

        /** @var WidgetModel $widgetModel */
        foreach ($widgetGroup->getWidgets() as $widgetModel) {
            $html .= $this->renderWidget($widgetModel);
        }

        $html .= ($class) ? '</div>' : '';

        return $html;
    }

    public function getWidget(string $widget): string
    {
        $widget = $this->getWidgetService()->getWidgetByName($widget);

        if (!$widget) return '';

        return $this->renderWidget($widget);
    }

    public function renderWidget(WidgetModel $widgetModel): string
    {
        $view           = $this->getView();
        $widgetClass    = $view->escapeHtml($widgetModel->getWidget());
        $html           = '';

        if ($this->getServiceLocator()->has($widgetClass)) {
            $html = $view->{$widgetClass}($widgetModel);
        }

        return $html;
    }

    protected function getWidgetGroupService(): WidgetGroupManager
    {
        if (!$this->widgetGroupManager instanceof WidgetGroupManager) {
            $widgetGroupManager = $this->getServiceLocator()
                ->getServiceLocator()
                ->get(ServiceManager::class)
                ->get(WidgetGroupManager::class);
            $this->widgetGroupManager = $widgetGroupManager;
        }

        return $this->widgetGroupManager;
    }

    protected function getWidgetService(): WidgetManager
    {
        if (!$this->widgetManager instanceof WidgetManager) {
            $widgetManager = $this->getServiceLocator()
                ->getServiceLocator()
                ->get('UthandoServiceManager')
                ->get(WidgetManager::class);
            $this->widgetManager = $widgetManager;
        }

        return $this->widgetManager;
    }
}<commit_msg>fixed service manger error<commit_after><?php declare(strict_types=1);
/**
 * Uthando CMS (http://www.shaunfreeman.co.uk/)
 *
 * @author      <NAME> <<EMAIL>>
 * @link        https://github.com/uthando-cms for the canonical source repository
 * @copyright   Copyright (c) 24/09/17 <NAME>. (http://www.shaunfreeman.co.uk)
 * @license     see LICENSE
 */

namespace UthandoThemeManager\View;

use UthandoCommon\Service\ServiceManager;
use UthandoCommon\View\AbstractViewHelper;
use UthandoThemeManager\Model\WidgetModel;
use UthandoThemeManager\Service\WidgetGroupManager;
use UthandoThemeManager\Service\WidgetManager;

class WidgetHelper extends AbstractViewHelper
{
    /**
     * @var WidgetGroupManager
     */
    protected $widgetGroupManager;

    /**
     * @var WidgetManager
     */
    protected $widgetManager;

    public function __invoke(string $name): string
    {
        $widgetOrGroup = strstr($name, '-', true);
        $widgetName = substr(strstr($name, '-'), 1);

        if ('group' === $widgetOrGroup) {
            $html = $this->getWidgetGroup($widgetName);
        } else {
            $html = $this->getWidget($name);
        }

        return $html;
    }

    public function getWidgetGroup(string $group): string
    {
        $widgetGroup = $this->getWidgetGroupService()->getWidgetGroupByName($group);

        if (!$widgetGroup) return '';

        $params         = $widgetGroup->parseParams();
        $class          = $params['class'] ?? '';
        $html           = ($class) ? '<div class="' . $this->getView()->escapeHtml($class) . '">' : '';

        /** @var WidgetModel $widgetModel */
        foreach ($widgetGroup->getWidgets() as $widgetModel) {
            $html .= $this->renderWidget($widgetModel);
        }

        $html .= ($class) ? '</div>' : '';

        return $html;
    }

    public function getWidget(string $widget): string
    {
        $widget = $this->getWidgetService()->getWidgetByName($widget);

        if (!$widget) return '';

        return $this->renderWidget($widget);
    }

    public function renderWidget(WidgetModel $widgetModel): string
    {
        $view           = $this->getView();
        $widgetClass    = $view->escapeHtml($widgetModel->getWidget());
        $html           = '';

        if ($this->getServiceLocator()->has($widgetClass)) {
            $html = $view->{$widgetClass}($widgetModel);
        }

        return $html;
    }

    protected function getWidgetGroupService(): WidgetGroupManager
    {
        if (!$this->widgetGroupManager instanceof WidgetGroupManager) {
            $widgetGroupManager = $this->getServiceLocator()
                ->getServiceLocator()
                ->get(ServiceManager::class)
                ->get(WidgetGroupManager::class);
            $this->widgetGroupManager = $widgetGroupManager;
        }

        return $this->widgetGroupManager;
    }

    protected function getWidgetService(): WidgetManager
    {
        if (!$this->widgetManager instanceof WidgetManager) {
            $widgetManager = $this->getServiceLocator()
                ->getServiceLocator()
                ->get(ServiceManager::class)
                ->get(WidgetManager::class);
            $this->widgetManager = $widgetManager;
        }

        return $this->widgetManager;
    }
}
{'fraction_non_alphanumeric': 0.1419257012888552, 'fraction_numerical': 0.0024260803639120547, 'mean_word_length': 2.4140786749482404, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 79, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '5208021', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1788, 'n_words': 331}