Repository navigation
Expand file tree
/
Copy pathMatcherServiceProvider.php
More file actions
72 lines (61 loc) · 2.31 KB
/
Copy pathMatcherServiceProvider.php
File metadata and controls
72 lines (61 loc) · 2.31 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
<?php
namespace LastCall\Crawler\Configuration\ServiceProvider;
use GuzzleHttp\Psr7\Uri;
use LastCall\Crawler\Uri\Matcher;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* Provides URL matching services for the crawler.
*/
class MatcherServiceProvider implements ServiceProviderInterface
{
/**
* The default set of HTML file extensions.
*
* @var array
*/
protected static $html_extensions = ['', 'html', 'htm', 'php', 'asp', 'aspx', 'cfm'];
/**
* The default set of asset file extensions.
*
* @var array
*/
protected static $asset_extensions = ['css', 'js', 'png', 'jpeg', 'jpg', 'svg'];
public function register(Container $pimple)
{
// File extensions used to determine whether a URI points to an asset
// or an HTML file.
$pimple['html_extensions'] = static::$html_extensions;
$pimple['asset_extensions'] = static::$asset_extensions;
// Matches any URI we think points to an HTML page.
$pimple['matcher.html'] = function () use ($pimple) {
return Matcher::all()
->pathExtensionIs($pimple['html_extensions']);
};
// Matches any URI we think points to an asset file.
$pimple['matcher.asset'] = function () use ($pimple) {
return Matcher::all()
->pathExtensionIs($pimple['asset_extensions']);
};
// Matches any URI that is considered "in scope."
$pimple['matcher.internal'] = function () use ($pimple) {
$uri = new Uri($pimple['base_url']);
return Matcher::all()
->schemeIs($uri->getScheme())
->hostIs($uri->getHost())
->pathMatches('~^'.preg_quote($uri->getPath(), '~').'~');
};
// Matches any URI that is both internal and HTML.
$pimple['matcher.internal_html'] = function () use ($pimple) {
return Matcher::all()
->add($pimple['matcher.internal'])
->add($pimple['matcher.html']);
};
// Matches any URI that is both internal and an asset.
$pimple['matcher.internal_asset'] = function () use ($pimple) {
return Matcher::all()
->add($pimple['matcher.internal'])
->add($pimple['matcher.asset']);
};
}
}