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
2424*
2525***/
2626
27+ use CodeIgniter \Config \BaseConfig ;
2728use CodeIgniter \Config \Services ;
29+ use Tatter \Assets \Exceptions \AssetsException ;
2830
2931/*** CLASS ***/
3032class 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}
0 commit comments