Skip to content

Instantly share code, notes, and snippets.

@aktuba aktuba/gist:7702229
Last active Dec 29, 2015

Embed
What would you like to do?
Первый вариант дополнения класса \Phalcon\Mvc\Url для реализации обратных ссылок. Основа взята из kohana 3.2
<?php
namespace Library;
class URL extends \Phalcon\Mvc\Url
{
const REGEX_KEY = ':([a-zA-Z0-9_]++)';
const REGEX_SEGMENT = '[^/.,;?\n]++';
const REGEX_ESCAPE = '[.\\+*?[^\\]${}=!|]';
public function base($protocol = NULL, $index = FALSE)
{
$base_url = trim($this->getDI()->get('url')->getBaseUri(), '/');
if (TRUE === $protocol)
{
$protocol = $this->getDI()->get('request')->getScheme();
}
if (!$protocol)
{
$protocol = parse_url($base_url, PHP_URL_SCHEME);
}
$index_file = $this->getDI()->get('config')->application->index_file;
if (TRUE === $index AND !empty($index_file))
{
$base_url .= $index_file . '/';
}
if (is_string($protocol))
{
if ($port = parse_url($base_url, PHP_URL_PORT))
{
$port = ':' . $port;
}
if ($domain = parse_url($base_url, PHP_URL_HOST))
{
$base_url = parse_url($base_url, PHP_URL_PATH);
}
else
{
$domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
}
$base_url = $protocol . '://' . $domain . $port . $base_url;
}
return rtrim($base_url, '/');
}
public function site($uri = '', $protocol = NULL, $index = TRUE)
{
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
if (preg_match('/[^\x00-\x7F]/S', $path))
{
$path = preg_replace_callback(
'~([^/]+)~',
function ($matches)
{
return rawurlencode($matches[0]);
},
$path);
}
return $this->base($protocol, $index) . (!empty($path) ? '/' . $path : '');
}
public function route($route, array $params = [])
{
$current = $this->_route($route);
$uri = $current['uri'];
$router = $this->getDI()->getShared('router');
$defaults = array(
'namespace' => $router->getNamespaceName(),
'module' => $router->getModuleName(),
'controller' => $router->getControllerName(),
'action' => $router->getActionName(),
);
$provided_optional = FALSE;
foreach ($params as $key => $arr)
{
if (is_array($arr))
{
$param = [];
foreach ($arr as $k => $v)
{
$param[] = $k.'/'.$v;
}
if (!empty($param))
{
$params[$key] = implode('/', $param);
}
}
}
while (preg_match('~\([^()]++\)(\?)?~', $uri, $match))
{
$search = $match[0];
$replace = substr($match[0], 1, empty($match[1]) ? -1 : -2);
$optional = !empty($match[1]);
while (preg_match('~'.URL::REGEX_KEY.'~', $replace, $match))
{
list($key, $param) = $match;
$default = isset($defaults[$param]) ? $defaults[$param] : '';
if (isset($params[$param]) && $params[$param] != $default)
{
$provided_optional = true;
$replace = str_replace($key, $params[$param], $replace);
}
elseif ($provided_optional && $optional)
{
if (isset($defaults[$param]))
{
$replace = str_replace($key, $default, $replace);
}
else
{
$replace = str_replace($key, '', $replace);
}
}
else
{
$replace = '';
break;
}
}
$uri = str_replace($search, $replace, $uri);
}
while (preg_match('~'.URL::REGEX_KEY.'~', $uri, $match))
{
list($key, $param) = $match;
if ( ! isset($params[$param]))
{
if (isset($defaults[$param]))
{
$params[$param] = $defaults[$param];
}
else
{
throw new \Phalcon\Exception('Required route parameter not passed: '.$param);
}
}
$uri = str_replace($key, $params[$param], $uri);
}
return preg_replace('~//+~', '/', rtrim($uri, '/'));
}
protected function _route($route)
{
static $_cache = [];
if (!isset($_cache[$route]))
{
$current_route = $this->getDI()->getShared('router')->getRouteByName($route);
if (empty($current_route))
{
throw new \Phalcon\Exception('Routes not found');
}
$uri = $current_route->getPattern();
if (empty($uri) || !is_string($uri))
{
throw new \Phalcon\Exception('Error route $expression');
}
$params = [];
while (preg_match('~\{([a-zA-Z0-9_]++):([^}]++)\}~uD', $uri, $match))
{
$params[trim($match[1])] = trim($match[2]);
$uri = str_replace('{'.$match[1].':'.$match[2].'}', ':'.trim($match[1]), $uri);
}
$expression = preg_replace('~'.URL::REGEX_ESCAPE.'~', '\\\\$0', $uri);
if (false !== strpos($expression, '('))
{
$expression = str_replace(array('(', ')\?'), array('(?:', ')?'), $expression);
}
while (preg_match('~:([a-zA-Z0-9_]++)~uD', $expression, $match))
{
$replace = isset($params[$match[1]]) ? $params[$match[1]] : ('params' != $match[1] ? URL::REGEX_SEGMENT : '.*');
$expression = str_replace(':'.$match[1], '(?P<'.$match[1].'>'.$replace.')', $expression);
}
$_cache[$route] = array(
'uri' => $uri,
'regex' => '~^'.$expression.'$~uD',
);
}
return $_cache[$route];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.