Skip to content

Commit 8e0b20a

Browse files
committed
Move to service, default config, HTML Helper
1 parent f0ca1b8 commit 8e0b20a

File tree

6 files changed

+92
-77
lines changed

6 files changed

+92
-77
lines changed

src/Config/Assets.php.example renamed to Assets.php.example

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313

1414
use CodeIgniter\Config\BaseConfig;
1515

16-
class Assets extends BaseConfig
16+
class Assets extends \Tatter\Assets\Config
1717
{
18-
// override location of asset files in the filesystem
19-
public $fsbase = FCPATH."assets/";
18+
// location of asset files in the filesystem
19+
public $fileBase = FCPATH . "assets/";
2020

21-
// override location of asset files URL
22-
public $webbase = "https://example.com/assets/";
21+
// location of asset files via URL
22+
public $webBase = base_url('assets/');
2323

2424
// additional assets to load per route - no leading/trailing slashes
2525
public $routes = [
2626
"" => [
2727
"bootstrap/dist/css/bootstrap.min.css",
2828
"bootstrap/dist/js/bootstrap.bundle.min.js",
2929
],
30-
"home" => [
30+
"upload" => [
3131
"dropzone/dropzone.min.css",
3232
"dropzone/dropzone.min.js",
3333
],

src/Assets.php

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Requirements:
1212
* >= PHP 7.2
1313
* >= CodeIgniter 4.0
14-
* CodeIgniter's Filesystem and URL helpers (loaded automatically)
14+
* CodeIgniter's Filesystem, HTML, and URL helpers (loaded automatically)
1515
*
1616
* Configuration:
1717
* Use Config/Assets.php for location overrides and custom route assets
@@ -24,29 +24,30 @@
2424
*
2525
***/
2626

27+
use CodeIgniter\Config\BaseConfig;
2728
use CodeIgniter\Config\Services;
29+
use Tatter\Assets\Exceptions\AssetsException;
2830

2931
/*** CLASS ***/
3032
class Assets
3133
{
32-
protected $fsbase;
33-
protected $webbase;
34-
protected $routes;
34+
/**
35+
* Our configuration instance.
36+
*
37+
* @var \Tatter\Assets\Config\Assets
38+
*/
39+
protected $config;
3540

3641
// initiate library, check for existing session
3742
public function __construct($config = null)
3843
{
3944
// load required helpers
4045
helper("filesystem");
46+
helper("html");
4147
helper("url");
4248

43-
// load optional configuration
44-
$config = $config ?? config('Assets', false);
45-
46-
// set parameters by config override or use default
47-
$this->fsbase = $config->fsbase ?? FCPATH."assets/";
48-
$this->webbase = $config->webbase ?? base_url()."assets/";
49-
$this->routes = $config->routes ?? [ ];
49+
// save configuration
50+
$this->config = $config;
5051
}
5152

5253
// returns route-relevant and preconfigured assets of a given extension
@@ -56,7 +57,7 @@ public function display(string $extension)
5657
if (! in_array($extension, ['css', 'js']))
5758
return false;
5859

59-
// output all matched files as tags
60+
// buffer all matched files as tags
6061
$buffer = "<!-- Local ".strtoupper($extension)." files -->".PHP_EOL;
6162
foreach ($this->routed($extension) as $file)
6263
$buffer .= $this->tag($file).PHP_EOL;
@@ -66,43 +67,44 @@ public function display(string $extension)
6667
// outputs a formatted tag for a single file
6768
public function displayFile(string $file)
6869
{
69-
if (! file_exists($this->fsbase . $file))
70+
if (! file_exists($this->config->fileBase . $file))
7071
return false;
7172
return $this->tag($file).PHP_EOL;
7273
}
7374

74-
// given (an existing) file, formats it as a vlid tag
75+
// given (an existing) file, formats it as a valid tag
7576
protected function tag(string $file)
7677
{
77-
$path = $this->fsbase . $file;
78+
$path = $this->config->fileBase . $file;
7879

7980
// get file extension
8081
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
8182

8283
// use last modified time for version control
8384
$version = filemtime($path);
8485

86+
// build the URL
87+
$url = $this->config->webBase . $file . "?v=" . $version;
88+
8589
// create extension-specific tag
8690
switch ($extension):
8791
case "css":
88-
$tag = "<link rel='stylesheet' href='{$this->webbase}{$file}?v={$version}' type='text/css' />";
92+
return link_tag($url);
8993
break;
9094

9195
case "js":
92-
$tag = "<script type='text/javascript' src='{$this->webbase}{$file}?v={$version}'></script>";
96+
return script_tag($url);
9397
break;
9498

9599
case "img":
96100
$alt = ucfirst(pathinfo($path, PATHINFO_FILENAME));
97-
$tag = "<img src='{$this->webbase}{$file}?v={$version}' alt='{$alt}' />";
101+
return img($url, false, ['alt' => $alt]);
98102
break;
99103

100104
default:
101-
throw new \Exception("Unsupported file extension: {$extension}");
105+
throw AssetsException::forUnsupportExtension($extension);
102106

103107
endswitch;
104-
105-
return $tag;
106108
}
107109

108110
// checks route-relevant folders and pre-configured array for relevant files with given extension
@@ -115,44 +117,46 @@ protected function routed($extension)
115117

116118
// get the controller (controllerName less its namespace)
117119
// accounts for default route and subdirectories
118-
$controller = str_replace([$routes->getDefaultNamespace(), '\\'], "", $router->controllerName());
120+
$controller = str_replace([$routes->getDefaultNamespace(), '\\'], '', $router->controllerName());
119121

120122
// start the route from the controller
121123
$segments = explode(PATH_SEPARATOR, $controller);
122124

123125
// add the method
124126
$segments[] = $router->methodName();
125127

126-
// add file-safe versions of parameters
128+
// add file-safe versions of any parameters
127129
foreach ($router->params() as $param)
128130
$segments[] = url_title($param);
129131

130132
// always start at base
131-
array_unshift($segments, "");
133+
array_unshift($segments, '');
132134

133135
// lowercase everything
134-
$segments = array_map("strtolower", $segments);
136+
$segments = array_map('strtolower', $segments);
135137

136138
// incrementally check each segment for files (matching this extension)
137-
$route = "";
139+
$route = '';
138140
$files = [ ];
139141
foreach ($segments as $segment):
140-
$route = empty($route)? $segment : $route."/".$segment; //prevents double slashes
142+
$route = empty($route)? $segment : $route . '/' . $segment; //prevents double slashes
141143

142-
// check for custom assets from config
144+
// check for custom assets from config first
143145
if (! empty($this->routes[$route]) ):
144146
foreach ($this->routes[$route] as $item):
147+
// make sure the extensions match
145148
if (is_string($item) && strtolower(pathinfo($item, PATHINFO_EXTENSION))==strtolower($extension)):
146149
$files[] = $item;
147150
endif;
148151
endforeach;
149152
endif;
150153

151154
// check filesystem for matching assets
152-
if ($items = directory_map($this->fsbase . $route, 1)):
155+
if ($items = directory_map($this->config->fileBase . $route, 1)):
153156
foreach ($items as $item):
157+
// make sure the extensions match
154158
if (strtolower(pathinfo($item, PATHINFO_EXTENSION))==strtolower($extension)):
155-
$files[] = empty($route)? $item : $route."/".$item;
159+
$files[] = empty($route)? $item : $route . '/' . $item;
156160
endif;
157161
endforeach;
158162
endif;
@@ -161,5 +165,4 @@ protected function routed($extension)
161165

162166
return array_unique($files);
163167
}
164-
165168
}

src/Config/Assets.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace Tatter\Assets\Config;
2+
3+
use CodeIgniter\Config\BaseConfig;
4+
5+
class Assets extends BaseConfig
6+
{
7+
// location of asset files in the filesystem
8+
public $fileBase = FCPATH . "assets/";
9+
10+
// location of asset files via URL
11+
public $webBase = base_url('assets/');
12+
13+
// additional assets to load per route - no leading/trailing slashes
14+
public $routes = [ ];
15+
}

src/Config/Services.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php namespace Tatter\Assets\Config;
2+
3+
use CodeIgniter\Config\BaseService;
4+
use CodeIgniter\View\RendererInterface;
5+
6+
class Services extends BaseService
7+
{
8+
public static function assets(BaseConfig $config = null, RendererInterface $view = null, bool $getShared = true)
9+
{
10+
if ($getShared):
11+
return static::getSharedInstance('assets', $config, $view);
12+
endif;
13+
14+
// prioritizes user config in app/Config if found
15+
if (empty($config)):
16+
if (class_exists('\Config\Assets')):
17+
$config = new \Config\Assets();
18+
else:
19+
$config = new \Tatter\Assets\Config\Assets();
20+
endif;
21+
endif;
22+
23+
return new \Tatter\Assets\Assets($config, $view);
24+
}
25+
}

src/Exceptions/AssetsException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace Tatter\Assets\Exceptions;
2+
3+
use CodeIgniter\Exceptions\ExceptionInterface;
4+
use CodeIgniter\Exceptions\FrameworkException;
5+
6+
class AssetsException extends FrameworkException implements ExceptionInterface
7+
{
8+
public static function forUnsupportExtension(string $extension = null)
9+
{
10+
return new static("Unsupported file extension: '{$extension}'");
11+
}
12+
}

src/Helpers/assets_helper.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)