Skip to content

Commit

Permalink
- Fixed issue with nullable prefetch value and nullable container or …
Browse files Browse the repository at this point in the history
…collection

- Added tests
  • Loading branch information
Machy8 committed Oct 17, 2017
1 parent e8d58fd commit 365f64f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/WebLoader/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function createCssFilesCollection(string $name): FilesCollection
throw new Exception('CSS files collection "' . $name . '" already exists.');
}

return $this->filesCollections[self::CSS][$name] = new FilesCollection;
return $this->filesCollections[self::CSS][$name] = new FilesCollection($name);
}


Expand Down Expand Up @@ -313,7 +313,7 @@ public function createJsFilesCollection(string $name): FilesCollection
throw new Exception('Javascript files collection "' . $name . '" already exists.');
}

return $this->filesCollections[self::JS][$name] = new FilesCollection;
return $this->filesCollections[self::JS][$name] = new FilesCollection($name);
}


Expand Down
17 changes: 17 additions & 0 deletions src/WebLoader/FilesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ class FilesCollection
*/
private $loadContent = FALSE;

/**
* @var string
*/
private $name;


public function __construct(string $name)
{
$this->name = $name;
}


public function enableContentLoading(): FilesCollection
{
Expand All @@ -73,6 +84,12 @@ public function getFilters(): array
}


public function getName(): string
{
return $this->name;
}


public function isContentLoadingEnabled(): bool
{
return $this->loadContent;
Expand Down
104 changes: 51 additions & 53 deletions src/WebLoader/FilesCollectionRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ public function __construct(array $collections, string $documentRoot, string $ba

public function css(string $collectionName = NULL, array $attributes = [], bool $loadContent = FALSE): string
{
$collectionName = $this->getSelectedCollectionName($collectionName);
$collection = $this->getCollection($collectionName, Compiler::CSS);
$collection = $this->getSelectedCollection($collectionName, Compiler::CSS);
$collectionName = $collection->getName();
$attributes = array_merge($attributes, $collection->getOutputElementAttributes());
$attributes['type'] = self::STYLE_TYPE_ATTRIBUTE;
$element = self::STYLE_ELEMENT;
$basePath = $this->getCollectionBasePath($collectionName, Compiler::CSS);
$filePathParameter = NULL;

if ($loadContent || $collection->isContentLoadingEnabled()) {
$filePathParameter = $this->getCollectionFilePath($basePath);
$filePathParameter = $this->getCollectionFilePath($basePath);

} else {
$element = self::LINK_ELEMENT;
Expand All @@ -95,47 +95,24 @@ public function css(string $collectionName = NULL, array $attributes = [], bool
}


public function cssPrefetch(array $collectionsNames): string
public function cssPrefetch(array $collectionsNames = []): string
{
return $this->generateMetaLinkElements($collectionsNames, Compiler::CSS, self::LINK_PREFETCH);
}


public function cssPreload(array $collectionsNames): string
public function cssPreload(array $collectionsNames = []): string
{
return $this->generateMetaLinkElements(
$collectionsNames, Compiler::CSS, self::LINK_PRELOAD, self::LINK_PRELOAD_AS_CSS
);
}


public function generateMetaLinkElements(
array $collectionsNames,
string $collectionsType,
string $rel,
string $as = NULL
): string {
$tags = '';
$attributes['rel'] = $rel;

if ($as) {
$attributes['as'] = $as;
}

foreach ($collectionsNames as $collectionName) {
$basePath = $this->getCollectionBasePath($collectionName, $collectionsType);
$attributes['href'] = $this->addVersionToBasePath($basePath);
$tags .= $this->generateElement(self::LINK_ELEMENT, $attributes);
}

return $tags;
}


public function js(string $collectionName = NULL, array $attributes = [], bool $loadContent = FALSE): string
{
$collectionName = $this->getSelectedCollectionName($collectionName);
$collection = $this->getCollection($collectionName, Compiler::JS);
$collection = $this->getSelectedCollection($collectionName, Compiler::JS);
$collectionName = $collection->getName();
$attributes = array_merge($attributes, $collection->getOutputElementAttributes());
$attributes['type'] = self::SCRIPT_TYPE_ATTRIBUTE;
$basePath = $this->getCollectionBasePath($collectionName, Compiler::JS);
Expand All @@ -152,13 +129,13 @@ public function js(string $collectionName = NULL, array $attributes = [], bool $
}


public function jsPrefetch(array $collectionsNames): string
public function jsPrefetch(array $collectionsNames = []): string
{
return $this->generateMetaLinkElements($collectionsNames, Compiler::JS, self::LINK_PREFETCH);
}


public function jsPreload(array $collectionsNames): string
public function jsPreload(array $collectionsNames = []): string
{
return $this->generateMetaLinkElements(
$collectionsNames, Compiler::JS, self::LINK_PRELOAD, self::LINK_PRELOAD_AS_JS
Expand Down Expand Up @@ -210,6 +187,33 @@ private function generateElement(string $element, array $attributes, string $fil
}


private function generateMetaLinkElements(
array $collectionsNames,
string $collectionsType,
string $rel,
string $as = NULL
): string {
$tags = '';
$attributes['rel'] = $rel;

if ($as) {
$attributes['as'] = $as;
}

if ( ! $collectionsNames) {
$collectionsNames[] = $this->getSelectedCollection(NULL, $collectionsType)->getName();
}

foreach ($collectionsNames as $collectionName) {
$basePath = $this->getCollectionBasePath($collectionName, $collectionsType);
$attributes['href'] = $this->addVersionToBasePath($basePath);
$tags .= $this->generateElement(self::LINK_ELEMENT, $attributes);
}

return $tags;
}


private function getCollectionBasePath(string $collectionName, string $type): string
{
$basePath = '/' . $collectionName . '.' . $type;
Expand All @@ -222,44 +226,38 @@ private function getCollectionBasePath(string $collectionName, string $type): st
}


private function getFileContent(string $path): string
private function getCollectionFilePath(string $path): string
{
return "\n" . file_get_contents($path) . "\n";
if ($this->documentRoot) {
$path = rtrim($this->documentRoot, '/') . $path;
}

return $path;
}


private function getCollectionFilePath(string $basePath): string
private function getFileContent(string $path): string
{
$filePath = '/' . $basePath;

if ($this->documentRoot) {
$filePath = '/' . $this->documentRoot . $filePath;
}

return $filePath;
return "\n" . file_get_contents($path) . "\n";
}


/**
* @throws Exception
*/
private function getCollection(string $collectionName = NULL, string $type): FilesCollection
private function getSelectedCollection(string $collectionName = NULL, string $type): FilesCollection
{
if ( ! array_key_exists($collectionName, $this->collections[$type])) {
throw new Exception('Trying to get undefined files collection "' . $collectionName . '".');
if ( ! $collectionName && ! $this->selectedCollectionName) {
throw new Exception('Trying to call files collection render on NULL.');
}

return $this->collections[$type][$collectionName];
}

$collectionName = $collectionName ?? $this->selectedCollectionName;

private function getSelectedCollectionName(string $collectionName = NULL): string
{
if ( ! $collectionName && ! $this->selectedCollectionName) {
throw new Exception('Trying to call files collection render on NULL.');
if ( ! array_key_exists($collectionName, $this->collections[$type])) {
throw new Exception('Trying to get undefined files collection "' . $collectionName . '".');
}

return $collectionName ?? $this->selectedCollectionName;
return $this->collections[$type][$collectionName];
}

}
14 changes: 7 additions & 7 deletions src/WebLoader/FilesCollectionsContainerRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(FilesCollectionRender $render, array $collectionsCon
public function css(string $containerName = NULL, array $attributes = [], bool $loadContent = FALSE): string
{
$cssElements = '';
$cssFilesCollections = $this->getContainer($containerName)->getCssCollections();
$cssFilesCollections = $this->getSelectedContainer($containerName)->getCssCollections();

foreach ($cssFilesCollections as $collectionName) {
$cssElements .= $this->render->css($collectionName, $attributes, $loadContent);
Expand All @@ -58,7 +58,7 @@ public function css(string $containerName = NULL, array $attributes = [], bool $

public function cssPrefetch(string $containerName = NULL, array $collectionsNames = []): string
{
$cssCollectionsFromContainer = $this->getContainer($containerName)->getCssCollections();
$cssCollectionsFromContainer = $this->getSelectedContainer($containerName)->getCssCollections();
$cssCollectionsNames = array_merge($cssCollectionsFromContainer, $collectionsNames);

return $this->render->cssPrefetch($cssCollectionsNames);
Expand All @@ -67,7 +67,7 @@ public function cssPrefetch(string $containerName = NULL, array $collectionsName

public function cssPreload(string $containerName = NULL, array $collectionsNames = []): string
{
$cssCollectionsFromContainer = $this->getContainer($containerName)->getCssCollections();
$cssCollectionsFromContainer = $this->getSelectedContainer($containerName)->getCssCollections();
$cssCollectionsNames = array_merge($cssCollectionsFromContainer, $collectionsNames);

return $this->render->cssPreload($cssCollectionsNames);
Expand All @@ -77,7 +77,7 @@ public function cssPreload(string $containerName = NULL, array $collectionsNames
public function js(string $containerName = NULL, array $attributes = [], bool $loadContent = FALSE): string
{
$jsElements = '';
$jsFilesCollections = $this->getContainer($containerName)->getJsCollections();
$jsFilesCollections = $this->getSelectedContainer($containerName)->getJsCollections();

foreach ($jsFilesCollections as $collectionName) {
$jsElements .= $this->render->js($collectionName, $attributes, $loadContent);
Expand All @@ -89,7 +89,7 @@ public function js(string $containerName = NULL, array $attributes = [], bool $l

public function jsPrefetch(string $containerName = NULL, array $collectionsNames = []): string
{
$jsCollectionsFromContainer = $this->getContainer($containerName)->getJsCollections();
$jsCollectionsFromContainer = $this->getSelectedContainer($containerName)->getJsCollections();
$jsCollectionsNames = array_merge($jsCollectionsFromContainer, $collectionsNames);

return $this->render->jsPrefetch($jsCollectionsNames);
Expand All @@ -98,7 +98,7 @@ public function jsPrefetch(string $containerName = NULL, array $collectionsNames

public function jsPreload(string $containerName = NULL, array $collectionsNames = []): string
{
$jsCollectionsFromContainer = $this->getContainer($containerName)->getJsCollections();
$jsCollectionsFromContainer = $this->getSelectedContainer($containerName)->getJsCollections();
$jsCollectionsNames = array_merge($jsCollectionsFromContainer, $collectionsNames);

return $this->render->jsPreload($jsCollectionsNames);
Expand All @@ -112,7 +112,7 @@ public function selectContainer(string $containerName): FilesCollectionsContaine
}


private function getContainer(string $containerName = NULL): FilesCollectionsContainer
private function getSelectedContainer(string $containerName = NULL): FilesCollectionsContainer
{
if ( ! $containerName && ! $this->selectedContainerName) {
throw new Exception('Trying to call files collections container render on NULL.');
Expand Down
16 changes: 16 additions & 0 deletions tests/ExceptionsTestCase.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ final class ExceptionsTestCase extends AbstractTestCase
}


public function testNullFilesCollectionsContainerException()
{
Assert::exception(function () {
$this->getWebLoader()->getFilesCollectionsContainerRender()->css();
}, Exception::class, 'Trying to call files collections container render on NULL.');
}


public function testNullFilesCollectionException()
{
Assert::exception(function () {
$this->getWebLoader()->getFilesCollectionRender()->jsPrefetch();
}, Exception::class, 'Trying to call files collection render on NULL.');
}


public function testUndefinedFilesCollectionsContainerException()
{
Assert::exception(function () {
Expand Down

0 comments on commit 365f64f

Please sign in to comment.