<commit_before><?php
/**
 * This file is part of the BEAR.Package package
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Package\Provide\ResourceView;

use BEAR\Resource\AbstractObject as ResourceObject;
use BEAR\Resource\AbstractObject;
use BEAR\Resource\DevInvoker;
use BEAR\Resource\Request;
use BEAR\Sunday\Extension\ResourceView\TemplateEngineRendererInterface;
use BEAR\Sunday\Extension\TemplateEngine\TemplateEngineAdapterInterface;
use BEAR\Sunday\Module\Cqrs\Interceptor\CacheLoader;
use DateInterval;
use DateTime;
use ReflectionClass;
use ReflectionObject;
use Traversable;
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;

/**
 * Request renderer
 *
 * @SuppressWarnings(PHPMD)
 */
class DevTemplateEngineRenderer implements TemplateEngineRendererInterface
{
    const NO_CACHE = '';
    const WRITE_CACHE = 'label-important';
    const READ_CACHE = 'label-success';
    const BADGE_ARGS = '<span class="badge badge-info">Arguments</span>';
    const BADGE_CACHE = '<span class="badge badge-info">Cache</span>';
    const BADGE_INTERCEPTORS = '<span class="badge badge-info">Interceptors</span>';
    const BADGE_PROFILE = '<span class="badge badge-info">Profile</span>';
    const ICON_LIFE = '<span class="icon-refresh"></span>';
    const ICON_TIME = '<span class="icon-time"></span>';
    const ICON_NA = '<span class="icon-ban-circle"></span>';
    const DIV_WELL = '<div style="padding:10px;">';

    /**
     * Template engine adapter
     *
     * @var TemplateEngineAdapterInterface
     */
    private $templateEngineAdapter;

    /**
     * BEAR.Package dir
     *
     * @var string
     */
    private $packageDir;

    /**
     * BEAR.Sunday dir
     *
     * @var string
     */
    private $sundayDir;

    /**
     * ViewRenderer Setter
     *
     * @param TemplateEngineAdapterInterface $templateEngineAdapter
     *
     * @Inject
     */
    public function __construct(TemplateEngineAdapterInterface $templateEngineAdapter)
    {
        $this->templateEngineAdapter = $templateEngineAdapter;
    }

    /**
     * Set packageDir
     *
     * @param string $packageDir
     *
     * @Inject
     * @Named("package_dir")
     */
    public function setPackageDir($packageDir)
    {
        $this->packageDir = $packageDir;
    }

    /**
     * Set sunday_dir
     *
     * @param string $sundayDir
     *
     * @Inject
     * @Named("sunday_dir")
     */
    public function setSundayDir($sundayDir)
    {
        $this->sundayDir = $sundayDir;
    }

    /**
     * {@inheritdoc}
     */
    public function render(AbstractObject $resourceObject)
    {
        if (is_scalar($resourceObject->body)) {
            $resourceObject->view = $resourceObject->body;
            return $resourceObject->body;
        }
        if (PHP_SAPI === 'cli') {
            // delegate original method to avoid render dev html.
            return (new TemplateEngineRenderer($this->templateEngineAdapter))->render($resourceObject);
        }
        // resource code editor
        $class = get_class($resourceObject);
        $pageFile = (new ReflectionClass($class))->getFileName();

        // resource template editor
        $dir = pathinfo($pageFile, PATHINFO_DIRNAME);
        $this->templateEngineAdapter->assign('resource', $resourceObject);
        if (is_array($resourceObject->body) || $resourceObject->body instanceof Traversable) {
            $this->templateEngineAdapter->assignAll($resourceObject->body);
        }
        $templateFileBase = $dir . DIRECTORY_SEPARATOR . substr(
            basename($pageFile),
            0,
            strlen(basename($pageFile)) - 3
        );

        // add tool bar
        $resourceObject->view = $body = $this->templateEngineAdapter->fetch($templateFileBase);
        $body = $this->addJsDevToolLadingHtml($body);
        $templateFile = $this->templateEngineAdapter->getTemplateFile();
        $templateFile = $this->makeRelativePath($templateFile);
        $label = $this->getLabel($body, $resourceObject, $templateFile);

        return $label;
    }

    /**
     * Return JS install html for dev tool
     *
     * @param string $body
     *
     * @return string<commit_msg>detect weaved class for fetch template<commit_after><?php
/**
 * This file is part of the BEAR.Package package
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Package\Provide\ResourceView;

use BEAR\Resource\AbstractObject as ResourceObject;
use BEAR\Resource\AbstractObject;
use BEAR\Resource\DevInvoker;
use BEAR\Resource\Request;
use BEAR\Sunday\Extension\ResourceView\TemplateEngineRendererInterface;
use BEAR\Sunday\Extension\TemplateEngine\TemplateEngineAdapterInterface;
use BEAR\Sunday\Module\Cqrs\Interceptor\CacheLoader;
use DateInterval;
use DateTime;
use ReflectionClass;
use ReflectionObject;
use Traversable;
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;

/**
 * Request renderer
 *
 * @SuppressWarnings(PHPMD)
 */
class DevTemplateEngineRenderer implements TemplateEngineRendererInterface
{
    const NO_CACHE = '';
    const WRITE_CACHE = 'label-important';
    const READ_CACHE = 'label-success';
    const BADGE_ARGS = '<span class="badge badge-info">Arguments</span>';
    const BADGE_CACHE = '<span class="badge badge-info">Cache</span>';
    const BADGE_INTERCEPTORS = '<span class="badge badge-info">Interceptors</span>';
    const BADGE_PROFILE = '<span class="badge badge-info">Profile</span>';
    const ICON_LIFE = '<span class="icon-refresh"></span>';
    const ICON_TIME = '<span class="icon-time"></span>';
    const ICON_NA = '<span class="icon-ban-circle"></span>';
    const DIV_WELL = '<div style="padding:10px;">';

    /**
     * Template engine adapter
     *
     * @var TemplateEngineAdapterInterface
     */
    private $templateEngineAdapter;

    /**
     * BEAR.Package dir
     *
     * @var string
     */
    private $packageDir;

    /**
     * BEAR.Sunday dir
     *
     * @var string
     */
    private $sundayDir;

    /**
     * ViewRenderer Setter
     *
     * @param TemplateEngineAdapterInterface $templateEngineAdapter
     *
     * @Inject
     */
    public function __construct(TemplateEngineAdapterInterface $templateEngineAdapter)
    {
        $this->templateEngineAdapter = $templateEngineAdapter;
    }

    /**
     * Set packageDir
     *
     * @param string $packageDir
     *
     * @Inject
     * @Named("package_dir")
     */
    public function setPackageDir($packageDir)
    {
        $this->packageDir = $packageDir;
    }

    /**
     * Set sunday_dir
     *
     * @param string $sundayDir
     *
     * @Inject
     * @Named("sunday_dir")
     */
    public function setSundayDir($sundayDir)
    {
        $this->sundayDir = $sundayDir;
    }

    /**
     * {@inheritdoc}
     */
    public function render(AbstractObject $resourceObject)
    {
        if (is_scalar($resourceObject->body)) {
            $resourceObject->view = $resourceObject->body;
            return $resourceObject->body;
        }
        if (PHP_SAPI === 'cli') {
            // delegate original method to avoid render dev html.
            return (new TemplateEngineRenderer($this->templateEngineAdapter))->render($resourceObject);
        }
        // resource code editor
        $class = get_class($resourceObject);
        $ref = new ReflectionClass($class);
        $pageFile = $ref->getFileName();
        if ($resourceObject instanceof \Ray\Aop\WeavedInterface) {
            $pageFile = $ref->getParentClass()->getFileName();
        }

        // resource template editor
        $dir = pathinfo($pageFile, PATHINFO_DIRNAME);
        $this->templateEngineAdapter->assign('resource', $resourceObject);
        if (is_array($resourceObject->body) || $resourceObject->body instanceof Traversable) {
            $this->templateEngineAdapter->assignAll($resourceObject->body);
        }
        $templateFileBase = $dir . DIRECTORY_SEPARATOR . substr(
            basename($pageFile),
            0,
            strlen(basename($pageFile)) - 3
        );

        // add tool bar
        $resourceObject->view = $body = $this->templateEngineAdapter->fetch($templateFileBase);
        $body = $this->addJsDevToolLadingHtml($body);
        $templateFile = $this->templateEngineAdapter->getTemplateFile();
        $templateFile = $this->makeRelativePath($templateFile);
        $label = $this->getLabel($body, $resourceObject, $templateFile);

        return $label;
    }

    /**
     * Return JS install html for dev tool
     *
     * @param string $body
     *
     * @return string
{'fraction_non_alphanumeric': 0.1266915538964069, 'fraction_numerical': 0.0009332711152589828, 'mean_word_length': 2.81191640729213, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 85, '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': '6647919', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2435, 'n_words': 482}