Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Nov 5, 2019
1 parent f50a698 commit 0510942
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/View/Helper/GlideHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ class GlideHelper extends Helper
*/
protected $_urlBuilder;

/**
* Webroot.
*
* @var bool
*/
protected $_webroot;

/**
* Initialize hook
*
* @param array $config Config
* @return void
*/
public function initialize(array $config)
{
if (method_exists($this->_View, 'getRequest')) {
$this->_webroot = $this->_View->getRequest()->getAttribute('webroot');
} else {
$this->_webroot = $this->request->getAttribute('webroot');
}
}

/**
* Creates a formatted IMG element.
*
Expand Down Expand Up @@ -81,11 +103,7 @@ public function url($path, array $params = [])
}
$url = $this->urlBuilder()->getUrl($path, $params);
if ($base && strpos($url, 'http') !== 0) {
if (method_exists($this->getView(), 'getRequest')) {
$url = $this->getView()->getRequest()->getAttribute('webroot') . ltrim($url, '/');
} else {
$url = $this->request->getAttribute('webroot') . ltrim($url, '/');
}
$url = $this->_webroot . ltrim($url, '/');
}

return $url;
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/View/Helper/GlideHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testUrl()
} else {
$this->helper->request = $this->helper->request->withAttribute('webroot', '/subfolder/');
}
$this->helper->initialize([]);

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

0 comments on commit 0510942

Please sign in to comment.