| 1 | <?php |
||
| 10 | class Url implements ScraperInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var \Aoe\Asdis\Domain\Model\Asset\Factory |
||
| 14 | */ |
||
| 15 | private $assetFactory; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param \Aoe\Asdis\Domain\Model\Asset\Factory $assetFactory |
||
| 19 | */ |
||
| 20 | 3 | public function injectAssetFactory(Factory $assetFactory) |
|
| 24 | |||
| 25 | /** |
||
| 26 | * @param $content |
||
| 27 | * @return \Aoe\Asdis\Domain\Model\Asset\Collection |
||
| 28 | */ |
||
| 29 | 3 | public function scrape($content) |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Extracts paths to resources in CSS code. |
||
| 37 | * This means file references which are included in "url(...)". |
||
| 38 | * |
||
| 39 | * @param string $cssContent |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | 3 | private function extractUrlPaths($cssContent) |
|
| 43 | { |
||
| 44 | 3 | $paths = []; |
|
| 45 | 3 | $masks = []; |
|
| 46 | 3 | $matches = []; |
|
| 47 | |||
| 48 | 3 | preg_match_all( |
|
| 49 | 3 | '~url\(\s*([\'"]?)(/?(\.\./)?.*?)([\'"]?);?\s*\)~is', |
|
| 50 | 3 | $cssContent, |
|
| 51 | 3 | $matches, |
|
| 52 | 3 | PREG_PATTERN_ORDER |
|
| 53 | ); |
||
| 54 | |||
| 55 | 3 | if (false === (is_array($matches) && sizeof($matches) > 1 && is_array($matches[2]))) { |
|
| 56 | return [ |
||
| 57 | 'paths' => $paths, |
||
| 58 | 'masks' => $masks |
||
| 59 | ]; |
||
| 60 | } |
||
| 61 | |||
| 62 | 3 | foreach ($matches[2] as $mkey => $path) { |
|
| 63 | 2 | if (strpos($path, ',') !== false) { |
|
| 64 | continue; |
||
| 65 | } |
||
| 66 | 2 | $paths[] = $path; |
|
| 67 | 2 | $masks[] = $matches[1][$mkey]; |
|
| 68 | } |
||
| 69 | |||
| 70 | return [ |
||
| 71 | 3 | 'paths' => $paths, |
|
| 72 | 3 | 'masks' => $masks |
|
| 73 | ]; |
||
| 75 | } |