Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleš committed Jul 20, 2017
1 parent fc7f1dd commit 0c3a7ea
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 294 deletions.
15 changes: 12 additions & 3 deletions src/Exceptions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

/**
* This file is part of the AlesWita\WebLoader
Expand All @@ -14,6 +14,15 @@
* @author Ales Wita
* @license MIT
*/
class WebLoaderException extends \Exception
{
class WebLoaderException extends \Exception
{
}


/**
* @author Ales Wita
* @license MIT
*/
class TestException extends \Exception
{
}
139 changes: 71 additions & 68 deletions src/Extension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

/**
* This file is part of the AlesWita\WebLoader
Expand All @@ -16,8 +16,8 @@
* @author Ales Wita
* @license MIT
*/
class Extension extends Nette\DI\CompilerExtension
{
class Extension extends Nette\DI\CompilerExtension
{
/** @var array */
public $defaults = [
'expiration' => null,
Expand All @@ -34,8 +34,8 @@ class Extension extends Nette\DI\CompilerExtension
/**
* @return void
*/
public function loadConfiguration(): void
{
public function loadConfiguration(): void
{
$config = $this->getConfig($this->defaults);
$container = $this->getContainerBuilder();

Expand All @@ -48,42 +48,42 @@ public function loadConfiguration(): void
->addSetup('$service->setCacheStorage(?)', [$container->getDefinitionByType('Nette\\Caching\\IStorage')])
->addSetup('$service->setHttpRequest(?)', [$container->getDefinitionByType('Nette\\Http\\IRequest')]);

if ($config['expiration'] !== null) {
$webLoader->addSetup('$service->setExpiration(?)', [$config['expiration']]);
if ($config['expiration'] !== null) {
$webLoader->addSetup('$service->setExpiration(?)', [$config['expiration']]);
}
if ($config['cache']['namespace'] !== null) {
$webLoader->addSetup('$service->setCacheNamespace(?)', [$config['cache']['namespace']]);
if ($config['cache']['namespace'] !== null) {
$webLoader->addSetup('$service->setCacheNamespace(?)', [$config['cache']['namespace']]);
}
if ($config['cache']['tag'] !== null) {
$webLoader->addSetup('$service->setCacheTag(?)', [$config['cache']['tag']]);
if ($config['cache']['tag'] !== null) {
$webLoader->addSetup('$service->setCacheTag(?)', [$config['cache']['tag']]);
}

if (is_array($config['folders'])) {
foreach ($config['folders'] as $folderSettings) {
if (!isset($folderSettings['originalFolder'])) {
throw new WebLoaderException('Missing parameter "originalFolder" in folder configuration!');
if (is_array($config['folders'])) {
foreach ($config['folders'] as $folderSettings) {
if (!isset($folderSettings['originalFolder'])) {
throw new WebLoaderException('Missing parameter "originalFolder" in folder configuration!');
}
if (!isset($folderSettings['tag'])) {
throw new WebLoaderException('Missing parameter "tag" in folder configuration!');
if (!isset($folderSettings['tag'])) {
throw new WebLoaderException('Missing parameter "tag" in folder configuration!');
}
if (!isset($folderSettings['namespace'])) {
$folderSettings['namespace'] = (array) Factory::DEFAULT_NAMESPACE;
if (!isset($folderSettings['namespace'])) {
$folderSettings['namespace'] = (array) Factory::DEFAULT_NAMESPACE;
}
if (!is_array($folderSettings['namespace'])) {
throw new WebLoaderException('Parameter "namespace" must be array in folder configuration!');
if (!is_array($folderSettings['namespace'])) {
throw new WebLoaderException('Parameter "namespace" must be array in folder configuration!');
}
if (!isset($folderSettings['masks'])) {
$folderSettings['masks'] = '*';
if (!isset($folderSettings['masks'])) {
$folderSettings['masks'] = '*';
}
if (!isset($folderSettings['limithDepth'])) {
$folderSettings['limithDepth'] = 0;
if (!isset($folderSettings['limithDepth'])) {
$folderSettings['limithDepth'] = 0;
}

$finder = Nette\Utils\Finder::findFiles($folderSettings['masks'])
->from($folderSettings['originalFolder'])
->limitDepth($folderSettings['limithDepth']);

foreach ($finder as $file) {
foreach ($finder as $file) {
$config['files'][] = [
//'originalFile' => $file->getLinkTarget(),// failed on Linux
'originalFile' => $file->getRealPath(),
Expand All @@ -92,39 +92,39 @@ public function loadConfiguration(): void
//'baseName' => basename($file->getLinkTarget()),// failed on Linux
'baseName' => basename($file->getRealPath()),
'folder' => (isset($folderSettings['folder']) ? $folderSettings['folder'] : null),
];
}
}
];
}
}
}

if (is_array($config['files'])) {
if (is_array($config['files'])) {
$files = [];
$allowedTags = [Factory::TAG_FILE_CSS, Factory::TAG_FILE_JS, Factory::TAG_FILE_OTHER];

foreach ($config['files'] as $fileSettings) {
if (!isset($fileSettings['originalFile'])) {
throw new WebLoaderException('Missing parameter "originalFile" in file configuration!');
foreach ($config['files'] as $fileSettings) {
if (!isset($fileSettings['originalFile'])) {
throw new WebLoaderException('Missing parameter "originalFile" in file configuration!');
}
if (!is_file($fileSettings['originalFile'])) {
throw new WebLoaderException('Can not find "' . $fileSettings['originalFile'] . '"!');
if (!is_file($fileSettings['originalFile'])) {
throw new WebLoaderException('Can not find "' . $fileSettings['originalFile'] . '"!');
}
if (!isset($fileSettings['tag'])) {
throw new WebLoaderException('Missing parameter "tag" in file configuration!');
if (!isset($fileSettings['tag'])) {
throw new WebLoaderException('Missing parameter "tag" in file configuration!');
}
if (!in_array($fileSettings['tag'], $allowedTags, true)) {
throw new WebLoaderException('Unknown file tag in configuration! Allowed tags: ' . implode(', ', $allowedTags));
if (!in_array($fileSettings['tag'], $allowedTags, true)) {
throw new WebLoaderException('Unknown file tag in configuration! Allowed tags: ' . implode(', ', $allowedTags));
}
if (!isset($fileSettings['namespace'])) {
$fileSettings['namespace'] = (array) Factory::DEFAULT_NAMESPACE;
if (!isset($fileSettings['namespace'])) {
$fileSettings['namespace'] = (array) Factory::DEFAULT_NAMESPACE;
}
if (!is_array($fileSettings['namespace'])) {
throw new WebLoaderException('Parameter "namespace" must be array in file configuration!');
if (!is_array($fileSettings['namespace'])) {
throw new WebLoaderException('Parameter "namespace" must be array in file configuration!');
}
if (!isset($fileSettings['baseName'])) {
$fileSettings['baseName'] = basename($fileSettings['originalFile']);
if (!isset($fileSettings['baseName'])) {
$fileSettings['baseName'] = basename($fileSettings['originalFile']);
}
if (isset($fileSettings['folder'])) {
$fileSettings['folder'] = Nette\Utils\Strings::trim($fileSettings['folder'], '\\/');
if (isset($fileSettings['folder'])) {
$fileSettings['folder'] = Nette\Utils\Strings::trim($fileSettings['folder'], '\\/');
}

$fileSettings['hash'] = md5_file($fileSettings['originalFile']);
Expand All @@ -143,38 +143,41 @@ public function loadConfiguration(): void
break;

case Factory::TAG_FILE_OTHER:
if (!isset($fileSettings['folder'])) {
throw new WebLoaderException('Missing parameter "folder" in file configuration! For tag "other" this tag is required.');
if (!isset($fileSettings['folder'])) {
throw new WebLoaderException('Missing parameter "folder" in file configuration! For tag "other" this tag is required.');
}

$fileSettings['file'] = $container->parameters['wwwDir'] . '/' . $fileSettings['folder'] . '/' . $fileSettings['baseName'];
$webLoader->addSetup('$service->addOtherFile(?)', [$fileSettings]);
break;
}

if (!isset($files[$fileSettings['folder']])) {
if (!isset($files[$fileSettings['folder']])) {
$files[$fileSettings['folder']][] = $fileSettings['baseName'];
} else {
if (in_array($fileSettings['baseName'], $files[$fileSettings['folder']], true)) {
} else {
if (in_array($fileSettings['baseName'], $files[$fileSettings['folder']], true)) {
throw new WebLoaderException('Folder "' . $fileSettings['folder'] . '" already have file with name "' . $fileSettings['baseName'] . '"!');
} else {
$files[$fileSettings['folder']][] = $fileSettings['baseName'];
}
}
}
} else {
$files[$fileSettings['folder']][] = $fileSettings['baseName'];
}
}
}
}

if (is_array($config['htmlTags'])) {
foreach ($config['htmlTags'] as $htmlTagSettings) {
if (!isset($htmlTagSettings['tag'])) {
throw new WebLoaderException('Missing parameter "tag" in HTML tag configuration!');
if (is_array($config['htmlTags'])) {
foreach ($config['htmlTags'] as $htmlTagSettings) {
if (!isset($htmlTagSettings['tag'])) {
throw new WebLoaderException('Missing parameter "tag" in HTML tag configuration!');
}
if (!isset($htmlTagSettings['namespace'])) {
$htmlTagSettings['namespace'] = (array) Factory::DEFAULT_NAMESPACE;
if (!isset($htmlTagSettings['namespace'])) {
$htmlTagSettings['namespace'] = (array) Factory::DEFAULT_NAMESPACE;
}
if (!is_array($htmlTagSettings['namespace'])) {
throw new WebLoaderException('Parameter "namespace" must be array in HTML tags configuration!');
}

$webLoader->addSetup('$service->addHtmlTag(?)', [$htmlTagSettings]);
}
}
}
}
$webLoader->addSetup('$service->addHtmlTag(?)', [$htmlTagSettings]);
}
}
}
}

0 comments on commit 0c3a7ea

Please sign in to comment.