|
29 | 29 | /*** CLASS ***/ |
30 | 30 | class Assets |
31 | 31 | { |
32 | | - |
33 | | - private $fsbase; |
34 | | - private $webbase; |
35 | | - private $routes; |
| 32 | + protected $fsbase; |
| 33 | + protected $webbase; |
| 34 | + protected $routes; |
36 | 35 |
|
37 | 36 | // initiate library, check for existing session |
38 | | - public function __construct() |
| 37 | + public function __construct($config = null) |
39 | 38 | { |
40 | 39 | // load required helpers |
41 | 40 | helper("filesystem"); |
42 | 41 | helper("url"); |
43 | 42 |
|
44 | 43 | // load optional configuration |
45 | | - $config = config('Assets', false); |
| 44 | + $config = $config ?? config('Assets', false); |
46 | 45 |
|
47 | | - // set parameters by config override or default |
| 46 | + // set parameters by config override or use default |
48 | 47 | $this->fsbase = $config->fsbase ?? FCPATH."assets/"; |
49 | 48 | $this->webbase = $config->webbase ?? base_url()."assets/"; |
50 | 49 | $this->routes = $config->routes ?? [ ]; |
51 | 50 | } |
52 | 51 |
|
53 | | - // outputs route-relevant and preconfigured assets of a given extension |
| 52 | + // returns route-relevant and preconfigured assets of a given extension |
54 | 53 | // accepts 'css' or 'js' |
55 | 54 | public function display(string $extension) |
56 | 55 | { |
57 | 56 | if (! in_array($extension, ['css', 'js'])) |
58 | 57 | return false; |
59 | 58 |
|
60 | 59 | // output all matched files as tags |
61 | | - echo "<!-- Local ".strtoupper($extension)." files -->".PHP_EOL; |
| 60 | + $buffer = "<!-- Local ".strtoupper($extension)." files -->".PHP_EOL; |
62 | 61 | foreach ($this->routed($extension) as $file) |
63 | | - echo $this->tag($file).PHP_EOL; |
| 62 | + $buffer .= $this->tag($file).PHP_EOL; |
| 63 | + return $buffer; |
64 | 64 | } |
65 | 65 |
|
66 | 66 | // outputs a formatted tag for a single file |
67 | | - public function displayFile(string $file) { |
| 67 | + public function displayFile(string $file) |
| 68 | + { |
68 | 69 | if (! file_exists($this->fsbase . $file)) |
69 | 70 | return false; |
70 | | - echo $this->tag($file).PHP_EOL; |
| 71 | + return $this->tag($file).PHP_EOL; |
71 | 72 | } |
72 | 73 |
|
73 | 74 | // given (an existing) file, formats it as a vlid tag |
|
0 commit comments