<commit_before><?php

namespace gear\components\gear\process;

use gear\Core;
use gear\interfaces\IProcess;
use gear\library\GComponent;

/**
 * Компонент обслуживающий процессы
 *
 * @package Gear Framework
 * @component ProcessComponent
 * @author <NAME>
 * @copyright <NAME>
 * @version 1.0.0
 * @since 03.08.2013
 * @php 5.4.x or higher
 * @release 1.0.0
 */
class GProcessManagerComponent extends GComponent
{
    /* Const */
    /* Private */
    /* Protected */
    /* Настройки компонента */
    protected static $_config = [];
    protected static $_init = false;
    /* Объект возвращающий параметры запроса */
    protected $_request = null;
    /* Имя процесса исполняемого по-умолчанию */
    protected $_defaultProcess = 'index';
    /* Префикс классов-процессов */
    protected $_prefixClassProcess = 'P';
    /* Список установленных процессов */
    protected $_processes = [];
    /* Текущий исполняемый процесс */
    protected $_currentProcess = null;
    /* Public */


    /**
     * Установка объекта отдающего параметры запроса
     *
     * @access public
     * @param object $request
     * @return $this
     */
    public function setRequest($request)
    {
        $this->_request = $request;
        return $this;
    }

    /**
     * Получение объекта отдающего параметры запроса
     *
     * @access public
     * @return null|object
     */
    public function getRequest() { return $this->_request; }

    /**
     * Исполнение процесса
     *
     * @access public
     * @param null|object|\Closure $process
     * @param null|object $request
     * @return mixed
     */
    public function exec($process = null, $request = null)
    {
        try {
            $this->request = !$request || !is_object($request) ? Core::app()->request : $request;
            if ($process && ($process instanceof IProcess || $process instanceof \Closure))
                $this->_currentProcess = $process;
            else
                $this->_currentProcess = $this->_routing();
        } catch (GException $e) {
            $this->trigger('onProcessNotFound', $e);
            return false;
        }
        $result = false;
        if (Core::trigger('onBeforeExec', $this->_currentProcess, $request)) {
            if ($this->_currentProcess instanceof \Closure) {
                Core::syslog('PROCESS MANAGER -> Execute closure process [' . __LINE__ . ']');
                $result = call_user_func($this->_currentProcess, $this->request);
                Core::trigger('onAfterExec', $this->_currentProcess, $result);
            } else {
                Core::syslog('PROCESS MANAGER -> Execute base process ' . get_class($this->_currentProcess) . ' [' . __LINE__ . ']');
                $result = $this->_currentProcess->entry($this->request);
            }
        }
        return $result;
    }

    /**
     * Получение процесса исходя из запроса пользователя
     *
     * @access protected
     * @return object of \gear\interfaces\IProcess or \Closure
     */
    protected function _routing()
    {
        $process = null;
        $processName = $this->request->get('e', $this->defaultProcess);
        Core::syslog('PROCESS MANAGER -> Prepare request process ' . $processName . ' [' . __LINE__ . ']');
        if (!$processName)
            throw $this->exceptionProcess('Unknown process');
        //$processes = $this->getProcesses();
        $class = null;
        $properties = [];
        if (isset($this->processes[$processName])) {
            $process = $this->processes[$processName];
            if (is_array($process)) {
                if (isset($process['class'])) {
                    /** @var array $config */
                    list($class, , $properties) = Core::getRecords($process);
                    $properties['name'] = $processName;
                } else
                    $properties = array_merge($process, ['name' => $processName]);
            } else
                $properties = ['name' => $processName, 'param' => $process];
        } else
            $properties = ['name' => $processName];
        if (!$class)
            $class = $this->_routingClass($processName);
        return $process ? $process : new $class($properties);
    }

    /**
     * Получение имя класса процесса
     *
     * @access protected
     * @param string $processName
     * @return string
     */
    protected function _routingClass($processName)
    {
        $routes = explode('/', $processName);
        $count = count($routes);
        $class = null;
        if ($count == 1)
            $class = Core::app()->getNamespace() . '\process\P' . ucfirst($processName);
        else
            if ($count == 2) {
                if ($processName[0] === '/')
                    $class = '\\' . $routes[0] . '\process\P' . ucfirst($routes[1]);
                else
                    $class = Core::app()->getNamespace() . '\modules\\' . $routes[0] . '\process\P' . ucfirst($routes[1]);
            } else
                if ($count >= 3)
                    $class = '\\' . $routes[0] . '\modules\\' . $routes[1] . '\process\P' . ucfirst($routes[2]);
        return $class;
    }

    /**
     * Установка описания процессов
     *
     * @access public
     * @param array $processes
     * @return $this
     */
    public function setProcesses(array $processes)
    {
        $this->_processes = $processes;
        return $this;
    }

    /**
     * Получение описания процессов<commit_msg>- bug fix<commit_after><?php

namespace gear\components\gear\process;

use gear\Core;
use gear\interfaces\IProcess;
use gear\library\GComponent;

/**
 * Компонент обслуживающий процессы
 *
 * @package Gear Framework
 * @component ProcessComponent
 * @author <NAME>
 * @copyright Kukush<NAME>
 * @version 1.0.0
 * @since 03.08.2013
 * @php 5.4.x or higher
 * @release 1.0.0
 */
class GProcessManagerComponent extends GComponent
{
    /* Const */
    /* Private */
    /* Protected */
    /* Настройки компонента */
    protected static $_config = [];
    protected static $_init = false;
    /* Объект возвращающий параметры запроса */
    protected $_request = null;
    /* Имя процесса исполняемого по-умолчанию */
    protected $_defaultProcess = 'index';
    /* Префикс классов-процессов */
    protected $_prefixClassProcess = 'P';
    /* Список установленных процессов */
    protected $_processes = [];
    /* Текущий исполняемый процесс */
    protected $_currentProcess = null;
    /* Public */


    /**
     * Установка объекта отдающего параметры запроса
     *
     * @access public
     * @param object $request
     * @return $this
     */
    public function setRequest($request)
    {
        $this->_request = $request;
        return $this;
    }

    /**
     * Получение объекта отдающего параметры запроса
     *
     * @access public
     * @return null|object
     */
    public function getRequest() { return $this->_request; }

    /**
     * Исполнение процесса
     *
     * @access public
     * @param null|object|\Closure $process
     * @param null|object $request
     * @return mixed
     */
    public function exec($process = null, $request = null)
    {
        try {
            $this->request = !$request || !is_object($request) ? Core::app()->request : $request;
            if ($process && ($process instanceof IProcess || $process instanceof \Closure))
                $this->_currentProcess = $process;
            else
                $this->_currentProcess = $this->_routing();
        } catch (GException $e) {
            $this->trigger('onProcessNotFound', $e);
            return false;
        }
        $result = false;
        if (Core::trigger('onBeforeExec', $this->_currentProcess, $request)) {
            if ($this->_currentProcess instanceof \Closure) {
                Core::syslog(__CLASS__ . ' -> Execute closure process [' . __LINE__ . ']');
                $result = call_user_func($this->_currentProcess, $this->request);
                Core::trigger('onAfterExec', $this->_currentProcess, $result);
            } else {
                Core::syslog(__CLASS__ . ' -> Execute base process ' . get_class($this->_currentProcess) . ' [' . __LINE__ . ']');
                $result = $this->_currentProcess->entry($this->request);
            }
        }
        return $result;
    }

    /**
     * Получение процесса исходя из запроса пользователя
     *
     * @access protected
     * @return object of \gear\interfaces\IProcess or \Closure
     */
    protected function _routing()
    {
        $process = null;
        $processName = $this->request->get('e', $this->defaultProcess);
        Core::syslog(__CLASS__ . ' -> Prepare request process ' . $processName . ' [' . __LINE__ . ']');
        if (!$processName)
            throw $this->exceptionProcess('Unknown process');
        //$processes = $this->getProcesses();
        $class = null;
        $properties = [];
        if (isset($this->processes[$processName])) {
            $process = $this->processes[$processName];
            if (is_array($process)) {
                if (isset($process['class'])) {
                    /** @var array $config */
                    list($class, , $properties) = Core::getRecords($process);
                    $properties['name'] = $processName;
                } else
                    $properties = array_merge($process, ['name' => $processName]);
            } else
                $properties = ['name' => $processName, 'param' => $process];
        } else
            $properties = ['name' => $processName];
        if (!$class)
            $class = $this->_routingClass($processName);
        return $process ? $process : new $class($properties);
    }

    /**
     * Получение имя класса процесса
     *
     * @access protected
     * @param string $processName
     * @return string
     */
    protected function _routingClass($processName)
    {
        $routes = explode('/', $processName);
        $count = count($routes);
        $class = null;
        if ($count == 1) {
            $class = Core::app()->getNamespace() . '\process\P' . ucfirst($processName);
            Core::syslog(__CLASS__ . ' -> Class routed ' . $class . ' [' . __LINE__ . ']');
        }
        else
            if ($count == 2) {
                if ($processName[0] === '/')
                    $class = '\\' . $routes[0] . '\process\P' . ucfirst($routes[1]);
                else
                    $class = Core::app()->getNamespace() . '\modules\\' . $routes[0] . '\process\P' . ucfirst($routes[1]);
            } else
                if ($count >= 3)
                    $class = '\\' . $routes[0] . '\modules\\' . $routes[1] . '\process\P' . ucfirst($routes[2]);
        return $class;
    }

    /**
     * Установка описания процессов
     *
     * @access public
     * @param array $processes
     * @return $this
     */
    public function setProcesses(array $processes)
    {
        $this->_processes = $processes;
        return $this;
    }

    /**
     * Получение описания процессов
{'fraction_non_alphanumeric': 0.14775510204081632, 'fraction_numerical': 0.004897959183673469, 'mean_word_length': 1.9767818574514038, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 80, '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': '7317917', 'n_tokens_mistral': 3244, 'n_tokens_neox': 3302, 'n_words': 695}