This repository was archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathModule.php
More file actions
96 lines (82 loc) · 2.18 KB
/
Module.php
File metadata and controls
96 lines (82 loc) · 2.18 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
<?php
namespace AssetLoader;
use Zend\Module\Manager,
Zend\EventManager\StaticEventManager;
/**
* Module for loading assets in development.
*/
class Module
{
/**
* Collected asset paths.
*
* @var array
*/
protected $assetPaths = array();
/**
* Initialize the module.
*
* @param Manager $moduleManager
* @return void
*/
public function init(Manager $moduleManager)
{
$moduleManager->events()->attach('loadModule', array($this, 'addAssetPath'));
$events = StaticEventManager::getInstance();
$events->attach('Zend\Mvc\Application', 'route', array($this, 'checkRequestUriForAsset'), PHP_INT_MAX);
}
/**
* Add an asset path from a module.
*
* @param Zend\EventManager\Event $event
* @return void
*/
public function addAssetPath($event)
{
$module = $event->getModule();
if (!method_exists($module, 'getAssetPath')) {
return;
}
if (null !== ($assetPath = $module->getAssetPath())) {
$this->assetPaths[] = rtrim($assetPath, '\\/');
}
}
/**
* Check a request for a valid file asset.
*
* @param Zend\EventManager\Event $event
* @return void
*/
public function checkRequestUriForAsset($event)
{
$request = $event->getRequest();
if (!method_exists($request, 'uri')) {
return;
}
if (method_exists($request, 'getBaseUrl')) {
$baseUrlLength = strlen($request->getBaseUrl() ?: '');
} else {
$baseUrlLength = 0;
}
$path = substr($request->uri()->getPath(), $baseUrlLength);
foreach ($this->assetPaths as $assetPath) {
if (file_exists($assetPath . $path)) {
$this->sendFile($assetPath . $path);
}
}
}
/**
* Send an asset file.
*
* @param string $file
* @return void
*/
protected function sendFile($filename)
{
$finfo = new finfo(FILEINFO_MIME);
$mimeType = $finfo->file($filename);
header('Content-Type: ' . $mimeType);
readfile($filename);
exit;
}
}