<commit_before><?php
/**
 * 
 *
 * All rights reserved.
 * 
 * @author <NAME>
 * @email <EMAIL>
 * @version 1.0
 * @company Studio107
 * @site http://studio107.ru
 * @date 12/05/14.05.2014 18:21
 */

namespace Mindy\Router;

use Exception;
use Yii;

class Patterns
{
    public $patterns = [];

    public $namespace = '';

    protected $parentPrefix;

    public function __construct($patterns, $namespace = '')
    {
        if(is_string($patterns)) {
            $tmp = Yii::getPathOfAlias($patterns);
            if(!is_file($tmp)) {
                $tmp = $patterns;
            }
            $patterns = require $tmp;
            if(!is_array($patterns)) {
                throw new Exception("Patterns must be a an array or alias to routes file");
            }
        }
        $this->patterns = $patterns;
        $this->namespace = $namespace;
    }

    public function setParentPrefix($prefix)
    {
        $this->parentPrefix = $prefix;
        return $this;
    }

    public function getPrefix($prefix)
    {
        return $this->parentPrefix ? $this->parentPrefix . $prefix : $prefix;
    }

    public function parse(array $patterns)
    {
        $className = __CLASS__;
        $routes = [];
        foreach($patterns as $urlPrefix => $params) {
            if($params instanceof $className) {
                /* @var $params Patterns */
                $routes = array_merge($routes, $params->setParentPrefix($urlPrefix)->getRoutes());
            } else {
                if(!array_key_exists('callback', $params)) {
                    continue;
                } else {
                    $callback = explode(':', $params['callback']);
                    list($controller, $action) = $callback;
                    $callbackParams = [
                        'controller' => $controller,
                        'action' => $action
                    ];

                    if(array_key_exists('values', $params)) {
                        $params['values'] = array_merge($params['values'], $callbackParams);
                    } else {
                        $params['values'] = $callbackParams;
                    }
                    unset($params['callback']);
                }

                $prefix = $this->parentPrefix ? $this->parentPrefix : $urlPrefix;
                if(!array_key_exists($prefix, $routes)) {
                    $routes[$prefix] = [
                        'routes' => []
                    ];
                }

                if(!empty($this->namespace)) {
                    $routes[$prefix]['name_prefix'] = $this->namespace . '.';
                }

                $name = $params['name'];
                unset($params['name']);
                if(count($params) == 0) {
                    $params = $urlPrefix;
                } else {
                    $params['path'] = $urlPrefix;
                }<commit_msg>Small up. Fix small errors.<commit_after><?php
/**
 * 
 *
 * All rights reserved.
 * 
 * @author <NAME>
 * @email <EMAIL>
 * @version 1.0
 * @company Studio107
 * @site http://studio107.ru
 * @date 12/05/14.05.2014 18:21
 */

namespace Mindy\Router;

use Exception;
use Yii;

class Patterns
{
    public $patterns = [];

    public $namespace = '';

    protected $parentPrefix;

    public function __construct($patterns, $namespace = '')
    {
        if(is_string($patterns)) {
            $tmp = Yii::getPathOfAlias($patterns);
            if(!$tmp) {
                $tmp = $patterns;
            } else {
                $tmp .= '.php';
            }

            if(is_file($tmp)) {
                $patterns = require $tmp;
            } else {
                throw new Exception("No such urls file $tmp");
            }

            if(!is_array($patterns)) {
                throw new Exception("Patterns must be a an array or alias to routes file: $patterns");
            }
        }
        $this->patterns = $patterns;
        $this->namespace = $namespace;
    }

    public function setParentPrefix($prefix)
    {
        $this->parentPrefix = $prefix;
        return $this;
    }

    public function getPrefix($prefix)
    {
        return $this->parentPrefix ? $this->parentPrefix . $prefix : $prefix;
    }

    public function parse(array $patterns)
    {
        $className = __CLASS__;
        $routes = [];
        foreach($patterns as $urlPrefix => $params) {
            if($params instanceof $className) {
                /* @var $params Patterns */
                $routes = array_merge($routes, $params->setParentPrefix($urlPrefix)->getRoutes());
            } else {
                if(!array_key_exists('callback', $params)) {
                    continue;
                } else {
                    if($urlPrefix[0] != '/') {
                        $urlPrefix = '/' . $urlPrefix;
                    }

                    $callback = explode(':', $params['callback']);
                    list($controller, $action) = $callback;
                    $callbackParams = [
                        'controller' => $controller,
                        'action' => $action
                    ];

                    if(array_key_exists('values', $params)) {
                        $params['values'] = array_merge($params['values'], $callbackParams);
                    } else {
                        $params['values'] = $callbackParams;
                    }
                    unset($params['callback']);
                }

                $prefix = $this->parentPrefix ? $this->parentPrefix : $urlPrefix;
                if(!array_key_exists($prefix, $routes)) {
                    $routes[$prefix] = [
                        'routes' => []
                    ];
                }

                if(!empty($this->namespace)) {
                    $routes[$prefix]['name_prefix'] = $this->namespace . '.';
                }

                $name = $params['name'];
                unset($params['name']);
                $params['path'] = $prefix == $urlPrefix ? '' : $urlPrefix;
{'fraction_non_alphanumeric': 0.14012632978723405, 'fraction_numerical': 0.008311170212765957, 'mean_word_length': 1.4068, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '7633632', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1536, 'n_words': 303}