Skip to content

Commit 0510942

Browse files
committed
Code optimization
1 parent f50a698 commit 0510942

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/View/Helper/GlideHelper.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ class GlideHelper extends Helper
4343
*/
4444
protected $_urlBuilder;
4545

46+
/**
47+
* Webroot.
48+
*
49+
* @var bool
50+
*/
51+
protected $_webroot;
52+
53+
/**
54+
* Initialize hook
55+
*
56+
* @param array $config Config
57+
* @return void
58+
*/
59+
public function initialize(array $config)
60+
{
61+
if (method_exists($this->_View, 'getRequest')) {
62+
$this->_webroot = $this->_View->getRequest()->getAttribute('webroot');
63+
} else {
64+
$this->_webroot = $this->request->getAttribute('webroot');
65+
}
66+
}
67+
4668
/**
4769
* Creates a formatted IMG element.
4870
*
@@ -81,11 +103,7 @@ public function url($path, array $params = [])
81103
}
82104
$url = $this->urlBuilder()->getUrl($path, $params);
83105
if ($base && strpos($url, 'http') !== 0) {
84-
if (method_exists($this->getView(), 'getRequest')) {
85-
$url = $this->getView()->getRequest()->getAttribute('webroot') . ltrim($url, '/');
86-
} else {
87-
$url = $this->request->getAttribute('webroot') . ltrim($url, '/');
88-
}
106+
$url = $this->_webroot . ltrim($url, '/');
89107
}
90108

91109
return $url;

tests/TestCase/View/Helper/GlideHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testUrl()
2929
} else {
3030
$this->helper->request = $this->helper->request->withAttribute('webroot', '/subfolder/');
3131
}
32+
$this->helper->initialize([]);
3233

3334
$result = $this->helper->url('logo.png', ['w' => 100]);
3435
$this->assertEquals('/subfolder/images/logo.png?w=100', $result);

0 commit comments

Comments
 (0)