Skip to content

Commit 38e90bf

Browse files
FedikQuyTonlaoneo
authored
[5.3] Deprecate HTMLHelper::script(), HTMLHelper::stylesheet(), extend lifetime for Document style/script methods (#43396)
* Deprecate HTMLHelper::script(), HTMLHelper::stylesheet() * Add HTMLHelper::mediaPath() * Add HTMLHelper::mediaPath() * Extend lifetime for Document style/script methods * Better debug handling * Better debug handling * Better debug handling * cs --------- Co-authored-by: Quy Ton <[email protected]> Co-authored-by: Allon Moritz <[email protected]>
1 parent 506f6ef commit 38e90bf

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

libraries/src/Document/Document.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Document
144144
* @var array
145145
* @since 1.7.0
146146
*
147-
* @deprecated 4.3 will be removed in 6.0
147+
* @deprecated 4.3 will be removed in 7.0
148148
* Use WebAssetManager
149149
*/
150150
public $_scripts = [];
@@ -155,7 +155,7 @@ class Document
155155
* @var array
156156
* @since 1.7.0
157157
*
158-
* @deprecated 4.3 will be removed in 6.0
158+
* @deprecated 4.3 will be removed in 7.0
159159
* Use WebAssetManager
160160
*/
161161
public $_script = [];
@@ -173,7 +173,7 @@ class Document
173173
* @var array
174174
* @since 1.7.0
175175
*
176-
* @deprecated 4.3 will be removed in 6.0
176+
* @deprecated 4.3 will be removed in 7.0
177177
* Use WebAssetManager
178178
*/
179179
public $_styleSheets = [];
@@ -184,7 +184,7 @@ class Document
184184
* @var array
185185
* @since 1.7.0
186186
*
187-
* @deprecated 4.3 will be removed in 6.0
187+
* @deprecated 4.3 will be removed in 7.0
188188
* Use WebAssetManager
189189
*/
190190
public $_style = [];
@@ -503,7 +503,7 @@ public function setMetaData($name, $content, $attribute = 'name')
503503
*
504504
* @since 1.7.0
505505
*
506-
* @deprecated 4.3 will be removed in 6.0
506+
* @deprecated 4.3 will be removed in 7.0
507507
* Use WebAssetManager
508508
* Example: $wa->registerAndUseScript(...);
509509
*/
@@ -530,7 +530,7 @@ public function addScript($url, $options = [], $attribs = [])
530530
*
531531
* @since 1.7.0
532532
*
533-
* @deprecated 4.3 will be removed in 6.0
533+
* @deprecated 4.3 will be removed in 7.0
534534
* Use WebAssetManager
535535
* Example: $wa->addInlineScript(...);
536536
*/
@@ -602,7 +602,7 @@ public function getScriptOptions($key = null)
602602
*
603603
* @since 1.7.0
604604
*
605-
* @deprecated 4.3 will be removed in 6.0
605+
* @deprecated 4.3 will be removed in 7.0
606606
* Use WebAssetManager
607607
* Example: $wa->registerAndUseStyle(...);
608608
*/
@@ -634,7 +634,7 @@ public function addStyleSheet($url, $options = [], $attribs = [])
634634
*
635635
* @since 1.7.0
636636
*
637-
* @deprecated 4.3 will be removed in 6.0
637+
* @deprecated 4.3 will be removed in 7.0
638638
* Use WebAssetManager
639639
* Example: $wa->addInlineStyle(...);
640640
*/

libraries/src/HTML/HTMLHelper.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,31 @@ public static function iframe($url, $name, $attribs = null, $noFrames = '')
348348
return '<iframe src="' . $url . '" ' . $attribs . ' name="' . $name . '">' . $noFrames . '</iframe>';
349349
}
350350

351+
/**
352+
* Look for path relatively to media folder.
353+
*
354+
* @param string $folder Folder name to search in (i.e. images, css, js).
355+
* @param string $file Path to file to check.
356+
* @param array $options Array with additional options:
357+
* relative: Flag if the path to the file is relative to the /media folder (and searches in template);
358+
* detectBrowser: Flag if the browser should be detected to include specific browser file;
359+
* detectDebug: Flag if debug mode is enabled to include uncompressed files (for css, js)
360+
* (boolean) - will enable debugging depends on site configuration, (1) - force debug On, (-1) - force debug Off;
361+
*
362+
* @return string
363+
* @since __DEPLOY_VERSION__
364+
*/
365+
final public static function mediaPath(string $folder, string $file, array $options = []): string
366+
{
367+
$relative = $options['relative'] ?? false;
368+
$detectBrowser = $options['detectBrowser'] ?? false;
369+
$detectDebug = $options['detectDebug'] ?? true;
370+
371+
$includes = static::includeRelativeFiles($folder, $file, $relative, $detectBrowser, $detectDebug);
372+
373+
return $includes[0] ?? '';
374+
}
375+
351376
/**
352377
* Compute the files to be included
353378
*
@@ -356,6 +381,7 @@ public static function iframe($url, $name, $attribs = null, $noFrames = '')
356381
* @param boolean $relative Flag if the path to the file is relative to the /media folder (and searches in template).
357382
* @param boolean $detectBrowser Flag if the browser should be detected to include specific browser files.
358383
* @param boolean $detectDebug Flag if debug mode is enabled to include uncompressed files if debug is on.
384+
* (boolean) - will enable debugging depends on site configuration, (1) - force debug On, (-1) - force debug Off;
359385
*
360386
* @return array files to be included.
361387
*
@@ -364,12 +390,17 @@ public static function iframe($url, $name, $attribs = null, $noFrames = '')
364390
*/
365391
protected static function includeRelativeFiles($folder, $file, $relative, $detectBrowser, $detectDebug)
366392
{
367-
// Set debug flag
368-
$debugMode = false;
369-
370393
// Detect debug mode
371-
if ($detectDebug && JDEBUG) {
372-
$debugMode = true;
394+
switch (true) {
395+
case \is_bool($detectDebug):
396+
$debugMode = $detectDebug && JDEBUG;
397+
break;
398+
case $detectDebug === 1:
399+
$debugMode = true;
400+
break;
401+
case $detectDebug === -1:
402+
default:
403+
$debugMode = false;
373404
}
374405

375406
// If http is present in filename
@@ -757,9 +788,14 @@ public static function image($file, $alt, $attribs = null, $relative = false, $r
757788
*
758789
* @see Browser
759790
* @since 1.5
791+
*
792+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
793+
* Use WebAssetManager::useStyle() or WebAssetManager::registerAndUseStyle() instead.
760794
*/
761795
public static function stylesheet($file, $options = [], $attribs = [])
762796
{
797+
@trigger_error('Method HTMLHelper::stylesheet() is deprecated, and will be removed in 7.0', \E_USER_DEPRECATED);
798+
763799
$options['relative'] = $options['relative'] ?? false;
764800
$options['pathOnly'] = $options['pathOnly'] ?? false;
765801
$options['detectBrowser'] = $options['detectBrowser'] ?? false;
@@ -804,9 +840,14 @@ public static function stylesheet($file, $options = [], $attribs = [])
804840
*
805841
* @see HTMLHelper::stylesheet()
806842
* @since 1.5
843+
*
844+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
845+
* Use WebAssetManager::useScript() or WebAssetManager::registerAndUseScript() instead.
807846
*/
808847
public static function script($file, $options = [], $attribs = [])
809848
{
849+
@trigger_error('Method HTMLHelper::script() is deprecated, and will be removed in 7.0', \E_USER_DEPRECATED);
850+
810851
$options['relative'] = $options['relative'] ?? false;
811852
$options['pathOnly'] = $options['pathOnly'] ?? false;
812853
$options['detectBrowser'] = $options['detectBrowser'] ?? false;

libraries/src/WebAsset/WebAssetItem.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,17 @@ protected function resolvePath(string $path, string $type): string
304304

305305
$file = $path;
306306
$external = $this->isPathExternal($path);
307+
$folders = ['script' => 'js', 'stylesheet' => 'css'];
307308

308309
if (!$external) {
309310
// Get the file path
310311
$file = HTMLHelper::_(
311-
$type,
312+
'mediaPath',
313+
$folders[$type],
312314
$path,
313315
[
314-
'pathOnly' => true,
315-
'relative' => !$this->isPathAbsolute($path),
316+
'detectDebug' => $this->getOption('debug') ? 1 : true,
317+
'relative' => !$this->isPathAbsolute($path),
316318
]
317319
);
318320
}

0 commit comments

Comments
 (0)