Skip to content

Commit

Permalink
Merge pull request #80 from jaapio/feature/fileAdapter
Browse files Browse the repository at this point in the history
extracted file interaction to adapter appraoch
  • Loading branch information
mvriel committed Aug 9, 2015
2 parents d7c824c + 5a9a818 commit 3516b97
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/phpDocumentor/Reflection/Php/Factory/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use InvalidArgumentException;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Php\Factory\File\Adapter;
use phpDocumentor\Reflection\Php\Factory\File\LocalAdapter;
use phpDocumentor\Reflection\Php\File as FileElement;
use phpDocumentor\Reflection\Php\NodesFactory;
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
Expand All @@ -41,12 +43,24 @@ final class File implements ProjectFactoryStrategy
private $nodesFactory;

/**
* Initializes the object
* @var Adapter
*/
private $adapter;

/**
* Initializes the object.
*
* @param NodesFactory $nodesFactory
* @param Adapter $adapter
*/
public function __construct(NodesFactory $nodesFactory)
public function __construct(NodesFactory $nodesFactory, Adapter $adapter = null)
{
if($adapter === null) {
$adapter = new LocalAdapter();
}

$this->nodesFactory = $nodesFactory;
$this->adapter = $adapter;
}

/**
Expand All @@ -57,7 +71,7 @@ public function __construct(NodesFactory $nodesFactory)
*/
public function matches($filePath)
{
return is_string($filePath) && file_exists($filePath);
return is_string($filePath) && $this->adapter->fileExists($filePath);
}

/**
Expand All @@ -80,11 +94,11 @@ public function create($object, StrategyContainer $strategies, Context $context
)
);
}
$code = file_get_contents($object);
$code = $this->adapter->getContents($object);
$nodes = $this->nodesFactory->create($code);
$docBlock = $this->createDocBlock($strategies, $code, $nodes);

$file = new FileElement(md5_file($object), $object, $code, $docBlock);
$file = new FileElement($this->adapter->md5($object), $this->adapter->path($object), $code, $docBlock);

$this->createElements(new Fqsen('\\'), $nodes, $file, $strategies);

Expand Down
51 changes: 51 additions & 0 deletions src/phpDocumentor/Reflection/Php/Factory/File/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* phpDocumentor
*
* PHP Version 5.5
*
* @copyright 2010-2015 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Php\Factory\File;

/**
* Interface for Adapters uses by the File strategy class to interact with the file system.
*/
interface Adapter
{

/**
* Returns true when the file exists.
*
* @param string $filePath
* @return boolean
*/
public function fileExists($filePath);

/**
* Returns the content of the file as a string.
*
* @param $filePath
* @return string
*/
public function getContents($filePath);

/**
* Returns md5 hash of the file.
*
* @param $filePath
* @return string
*/
public function md5($filePath);

/**
* Returns an relative path to the file.
*
* @param string $filePath
* @return string
*/
public function path($filePath);
}
63 changes: 63 additions & 0 deletions src/phpDocumentor/Reflection/Php/Factory/File/LocalAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* phpDocumentor
*
* PHP Version 5.5
*
* @copyright 2010-2015 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Php\Factory\File;

/**
* Adapter to interact with local readable files.
*/
final class LocalAdapter implements Adapter
{

/**
* Returns true when the file exists.
*
* @param string $filePath
* @return boolean
*/
public function fileExists($filePath)
{
return file_exists($filePath);
}

/**
* Returns the content of the file as a string.
*
* @param $filePath
* @return string
*/
public function getContents($filePath)
{
return file_get_contents($filePath);
}

/**
* Returns md5 hash of the file.
*
* @param $filePath
* @return string
*/
public function md5($filePath)
{
return md5_file($filePath);
}

/**
* Returns an relative path to the file.
*
* @param string $filePath
* @return string
*/
public function path($filePath)
{
return $filePath;
}
}

0 comments on commit 3516b97

Please sign in to comment.