-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLSite.php
More file actions
executable file
·123 lines (101 loc) · 2.97 KB
/
URLSite.php
File metadata and controls
executable file
·123 lines (101 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package Kohana/Codebench
* @category Tests
* @author Geert De Deckere <geert@idoe.be>
*/
class Bench_URLSite extends Codebench {
public $description = 'http://dev.kohanaframework.org/issues/3110';
public $loops = 1000;
public $subjects = array
(
'',
'news',
'news/',
'/news/',
'news/page/5',
'news/page:5',
'http://example.com/',
'http://example.com/hello',
'http://example.com:80/',
'http://user:pass@example.com/',
);
public function __construct()
{
foreach ($this->subjects as $subject)
{
// Automatically create URIs with query string and/or fragment part appended
$this->subjects[] = $subject.'?query=string';
$this->subjects[] = $subject.'#fragment';
$this->subjects[] = $subject.'?query=string#fragment';
}
parent::__construct();
}
public function bench_original($uri)
{
// Get the path from the URI
$path = trim(parse_url($uri, PHP_URL_PATH), '/');
if ($query = parse_url($uri, PHP_URL_QUERY))
{
$query = '?'.$query;
}
if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
{
$fragment = '#'.$fragment;
}
return $path.$query.$fragment;
}
public function bench_explode($uri)
{
// Chop off possible scheme, host, port, user and pass parts
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
$fragment = '';
$explode = explode('#', $path, 2);
if (isset($explode[1]))
{
$path = $explode[0];
$fragment = '#'.$explode[1];
}
$query = '';
$explode = explode('?', $path, 2);
if (isset($explode[1]))
{
$path = $explode[0];
$query = '?'.$explode[1];
}
return $path.$query.$fragment;
}
public function bench_regex($uri)
{
preg_match('~^(?:[-a-z0-9+.]++://[^/]++/?)?([^?#]++)?(\?[^#]*+)?(#.*)?~', trim($uri, '/'), $matches);
$path = Arr::get($matches, 1, '');
$query = Arr::get($matches, 2, '');
$fragment = Arr::get($matches, 3, '');
return $path.$query.$fragment;
}
public function bench_regex_without_arrget($uri)
{
preg_match('~^(?:[-a-z0-9+.]++://[^/]++/?)?([^?#]++)?(\?[^#]*+)?(#.*)?~', trim($uri, '/'), $matches);
$path = isset($matches[1]) ? $matches[1] : '';
$query = isset($matches[2]) ? $matches[2] : '';
$fragment = isset($matches[3]) ? $matches[3] : '';
return $path.$query.$fragment;
}
// And then I thought, why do all the work of extracting the query and fragment parts and then reappending them?
// Just leaving them alone should be fine, right? As a bonus we get a very nice speed boost.
public function bench_less_is_more($uri)
{
// Chop off possible scheme, host, port, user and pass parts
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
return $path;
}
public function bench_less_is_more_with_strpos_optimization($uri)
{
if (strpos($uri, '://') !== FALSE)
{
// Chop off possible scheme, host, port, user and pass parts
$uri = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
}
return $uri;
}
}