Skip to content

Commit

Permalink
Began refactoring ViewFile for #320
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed Aug 18, 2018
1 parent cccefdf commit 2924d1d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
37 changes: 10 additions & 27 deletions src/Entities/ViewFile.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Tapestry\Entities;
use Tapestry\Modules\Source\AbstractSource;

use Tapestry\Modules\Source\SourceInterface;

/**
* Class ViewFile.
Expand All @@ -15,43 +16,25 @@ class ViewFile
use ViewFileTrait;

/**
* @var Project
* @deprecated
*/
private $project;

/**
* @var string
* @var SourceInterface
*/
private $fileUid;

/**
* @var AbstractSource
*/
private $file;
private $source;

/**
* ViewFile constructor.
*
* @todo remove dependence on Project and only pass in AbstractSource
* @param Project $project
* @param $fileUid
* @param SourceInterface $source
*/
public function __construct(Project $project, $fileUid)
public function __construct(SourceInterface $source)
{
$this->project = $project;
$this->fileUid = $fileUid;
$this->source = $source;
}

/**
* @return AbstractSource
* @return SourceInterface
*/
public function getSource(): AbstractSource
public function getSource(): SourceInterface
{
if (is_null($this->file)) {
$this->file = $this->project->get('compiled.'.$this->fileUid);
}

return $this->file;
return $this->source;
}
}
6 changes: 3 additions & 3 deletions src/Entities/ViewFileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract public function getSource();
*/
public function getData($key, $default = null)
{
return $this->getFile()->getData($key, $default);
return $this->getSource()->getData($key, $default);
}

/**
Expand All @@ -38,7 +38,7 @@ public function getData($key, $default = null)
*/
public function getPermalink()
{
return $this->getFile()->getCompiledPermalink();
return $this->getSource()->getCompiledPermalink();
}

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getContent()
return $content;
}

return $this->getFile()->getContent();
return $this->getSource()->getContent();
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/ViewFileNTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tapestry\Tests\Unit;

use Tapestry\Entities\ViewFile;
use Tapestry\Modules\Source\MemorySource;
use Tapestry\Tests\TestCase;

class ViewFileNTest extends TestCase
{

public function testViewFile()
{

try{
$memory = new MemorySource('template', '', 'template.phtml', 'phtml', '/', '/template.phtml');

$viewFile = new ViewFile($memory);
$this->assertSame($memory, $viewFile->getSource());
$this->assertSame($memory->getData('uid'), $viewFile->getData('uid'));
$this->assertSame($memory->getCompiledPermalink(), $viewFile->getPermalink());

// @todo complete this for #320
//$this->assertSame(url($memory->getPermalink()), $viewFile->getUrl());
//$this->assertSame($memory->getData('date'), $viewFile->getDate());
//$this->assertSame($memory->getData('categories', []), $viewFile->taxonomyList('categories'));

} catch (\Exception $e) {
$this->fail($e);
}

}

}

0 comments on commit 2924d1d

Please sign in to comment.