Skip to content

Commit

Permalink
Inline Options
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Aug 24, 2020
1 parent da6dd10 commit 3e951bf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
25 changes: 15 additions & 10 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function requireJs($toRequire, ?array $options = [], int $priority = Reso
{
if($this->_type == self::MAP_INLINE)
{
return $this->_requireInlineJs($toRequire, $priority);
return $this->_requireInlineJs($toRequire, $options, $priority);
}
$this->getResourceStore()->requireJs($this->getResourceUri($toRequire, false), $options, $priority);
return $this;
Expand All @@ -254,15 +254,17 @@ public function requireJs($toRequire, ?array $options = [], int $priority = Reso
/**
* Add a js script to the store
*
* @param $javascript
* @param $javascript
*
* @param int $priority
* @param array|null $options
* @param int $priority
*
* @return ResourceManager
*/
protected function _requireInlineJs($javascript, int $priority = ResourceStore::PRIORITY_DEFAULT)
protected function _requireInlineJs($javascript, ?array $options = [], int $priority = ResourceStore::PRIORITY_DEFAULT
)
{
$this->getResourceStore()->requireInlineJs($javascript, $priority);
$this->getResourceStore()->requireInlineJs($javascript, $options, $priority);
return $this;
}

Expand Down Expand Up @@ -474,7 +476,7 @@ public function requireCss($toRequire, ?array $options = [], int $priority = Res
{
if($this->_type == self::MAP_INLINE)
{
return $this->_requireInlineCss($toRequire, $priority);
return $this->_requireInlineCss($toRequire, $options, $priority);
}
$this->getResourceStore()->requireCss($this->getResourceUri($toRequire, false), $options, $priority);
return $this;
Expand All @@ -483,15 +485,18 @@ public function requireCss($toRequire, ?array $options = [], int $priority = Res
/**
* Add css to the store
*
* @param $stylesheet
* @param $stylesheet
*
* @param int $priority
* @param array|null $options
* @param int $priority
*
* @return ResourceManager
*/
protected function _requireInlineCss($stylesheet, int $priority = ResourceStore::PRIORITY_DEFAULT)
protected function _requireInlineCss(
$stylesheet, ?array $options = [], int $priority = ResourceStore::PRIORITY_DEFAULT
)
{
$this->getResourceStore()->requireInlineCss($stylesheet, $priority);
$this->getResourceStore()->requireInlineCss($stylesheet, $options, $priority);
return $this;
}
}
39 changes: 29 additions & 10 deletions src/ResourceStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,30 @@ public function generateHtmlIncludes($for = self::TYPE_CSS, int $priority = null
if(strlen($uri) == 32 && !stristr($uri, '/'))
{
$inlineContent = isset($options['_']) ? $options['_'] : null;
$attrs = [];
foreach($options as $opt => $optV)
{
if($opt === '_' || $opt === 'rel')
{
continue;
}
if($optV === true)
{
$attrs[] = $opt;
}
else
{
$attrs[] = "$opt='" . \htmlspecialchars($optV, ENT_QUOTES, 'UTF-8') . "'";
}
}
$attr = empty($attrs) ? '' : ' ' . implode(' ', $attrs);
if($for == self::TYPE_CSS)
{
$return .= '<style>' . $inlineContent . '</style>';
$return .= '<style' . $attr . '>' . $inlineContent . '</style>';
}
else if($for == self::TYPE_JS)
{
$return .= '<script>' . $inlineContent . '</script>';
$return .= '<script' . $attr . '>' . $inlineContent . '</script>';
}
}
else if(!empty($uri))
Expand Down Expand Up @@ -236,12 +253,13 @@ protected function _defaultOptions(string $type, ?array $options, int $priority)
/**
* Add a js script to the store
*
* @param $javascript
* @param int $priority
* @param $javascript
* @param array|null $options
* @param int $priority
*/
public function requireInlineJs($javascript, int $priority = self::PRIORITY_DEFAULT)
public function requireInlineJs($javascript, ?array $options = [], int $priority = self::PRIORITY_DEFAULT)
{
$this->addResource(self::TYPE_JS, md5($javascript), ['_' => $javascript], $priority);
$this->addResource(self::TYPE_JS, md5($javascript), array_merge($options ?? [], ['_' => $javascript]), $priority);
}

/**
Expand All @@ -263,11 +281,12 @@ public function requireCss($filename, ?array $options = [], int $priority = self
/**
* Add css to the store
*
* @param $stylesheet
* @param int $priority
* @param $stylesheet
* @param array|null $options
* @param int $priority
*/
public function requireInlineCss($stylesheet, int $priority = self::PRIORITY_DEFAULT)
public function requireInlineCss($stylesheet, ?array $options = [], int $priority = self::PRIORITY_DEFAULT)
{
$this->addResource(self::TYPE_CSS, md5($stylesheet), ['_' => $stylesheet], $priority);
$this->addResource(self::TYPE_CSS, md5($stylesheet), array_merge($options ?? [], ['_' => $stylesheet]), $priority);
}
}
4 changes: 2 additions & 2 deletions tests/ResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public function testRequireInlineCss()
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
ResourceManager::inline()->requireCss("body{background:green;}");
$this->assertEquals(
'<style>body{background:green;}</style>',
'<style type=\'text/css\'>body{background:green;}</style>',
Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_CSS)
);
ResourceManager::inline()->requireCss("body{background:red;}", null, ResourceStore::PRIORITY_HIGH);
$this->assertEquals(
'<style>body{background:red;}</style><style>body{background:green;}</style>',
'<style type=\'text/css\'>body{background:red;}</style><style type=\'text/css\'>body{background:green;}</style>',
Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_CSS)
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ResourceStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function testGenerateHtmlIncludes()
);
$this->assertContains('<script>alert(\'hi\');</script>', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
$this->assertContains(
'<style>body{background:red;}</style>',
'<style type=\'text/css\'>body{background:red;}</style>',
$store->generateHtmlIncludes(ResourceStore::TYPE_CSS)
);
}
Expand Down

0 comments on commit 3e951bf

Please sign in to comment.