Skip to content

Commit

Permalink
Do not pass file as StdClass, use a File object instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed Sep 4, 2018
1 parent f1df18c commit 5a8904a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
6 changes: 1 addition & 5 deletions WebLoader/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,7 @@ protected function generateFiles(array $files, array $watchFiles = [])
file_put_contents($outPath, $this->getContent($files));
}

return (object) [
'file' => $name,
'lastModified' => filemtime($path),
'sourceFiles' => $files,
];
return new File($name, filemtime($path), $files);
}


Expand Down
7 changes: 5 additions & 2 deletions WebLoader/Nette/Diagnostics/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Latte\Runtime\Filters;
use Tracy\Debugger;
use WebLoader\Compiler;
use WebLoader\File;

/**
* Debugger panel.
Expand Down Expand Up @@ -86,12 +87,14 @@ private function compute(): array
}

$compilerCombinedSize = 0;

/** @var $generated File */
foreach ($compiler->generate() as $generated) {
$generatedSize = filesize($compiler->getOutputDir() . DIRECTORY_SEPARATOR . $generated->file);
$generatedSize = filesize($compiler->getOutputDir() . DIRECTORY_SEPARATOR . $generated->getFile());
$size['combined'] += $generatedSize;
$compilerCombinedSize += $generatedSize;

foreach ($generated->sourceFiles as $file) {
foreach ($generated->getSourceFiles() as $file) {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$file = str_replace('\\', DIRECTORY_SEPARATOR, realpath($file));

Expand Down
7 changes: 4 additions & 3 deletions WebLoader/Nette/WebLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\Utils\Html;
use WebLoader\Compiler;
use WebLoader\File;
use WebLoader\FileCollection;

/**
Expand Down Expand Up @@ -95,12 +96,12 @@ public function render(): void
}


protected function getGeneratedFilePath(string $file)
protected function getGeneratedFilePath(File $file)
{
$path = $this->tempPath . '/' . $file->file;
$path = $this->tempPath . '/' . $file->getFile();

if ($this->appendLastModified) {
$path .= '?' . $file->lastModified;
$path .= '?' . $file->getLastModified();
}

return $path;
Expand Down
17 changes: 12 additions & 5 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Mockery;
use PHPUnit\Framework\TestCase;
use WebLoader\Compiler;
use WebLoader\File;

/**
* CompilerTest
Expand Down Expand Up @@ -111,22 +112,28 @@ public function testGeneratingAndFilters(): void
$expectedContent = '-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL .
'c:ihg,-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,';

/**
* @var $files File[]
*/
$files = $this->object->generate();

$this->assertTrue(is_numeric($files[0]->lastModified) && $files[0]->lastModified > 0, 'Generate does not provide last modified timestamp correctly.');
$this->assertTrue(is_numeric($files[0]->getLastModified()) && $files[0]->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.');

$content = file_get_contents($this->object->getOutputDir() . '/' . $files[0]->file);
$content = file_get_contents($this->object->getOutputDir() . '/' . $files[0]->getFile());

$this->assertEquals($expectedContent, $content);
}


public function testGenerateReturnsSourceFilePaths(): void
{
/**
* @var $res File[]
*/
$res = $this->object->generate();
$this->assertInternalType('array', $res[0]->sourceFiles);
$this->assertCount(3, $res[0]->sourceFiles);
$this->assertFileExists($res[0]->sourceFiles[0]);
$this->assertInternalType('array', $res[0]->getSourceFiles());
$this->assertCount(3, $res[0]->getSourceFiles());
$this->assertFileExists($res[0]->getSourceFiles()[0]);
}


Expand Down

0 comments on commit 5a8904a

Please sign in to comment.