Skip to content

Commit 3516b97

Browse files
committed
Merge pull request #80 from jaapio/feature/fileAdapter
extracted file interaction to adapter appraoch
2 parents d7c824c + 5a9a818 commit 3516b97

File tree

3 files changed

+133
-5
lines changed

3 files changed

+133
-5
lines changed

src/phpDocumentor/Reflection/Php/Factory/File.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use InvalidArgumentException;
1717
use phpDocumentor\Reflection\Fqsen;
18+
use phpDocumentor\Reflection\Php\Factory\File\Adapter;
19+
use phpDocumentor\Reflection\Php\Factory\File\LocalAdapter;
1820
use phpDocumentor\Reflection\Php\File as FileElement;
1921
use phpDocumentor\Reflection\Php\NodesFactory;
2022
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
@@ -41,12 +43,24 @@ final class File implements ProjectFactoryStrategy
4143
private $nodesFactory;
4244

4345
/**
44-
* Initializes the object
46+
* @var Adapter
47+
*/
48+
private $adapter;
49+
50+
/**
51+
* Initializes the object.
52+
*
4553
* @param NodesFactory $nodesFactory
54+
* @param Adapter $adapter
4655
*/
47-
public function __construct(NodesFactory $nodesFactory)
56+
public function __construct(NodesFactory $nodesFactory, Adapter $adapter = null)
4857
{
58+
if($adapter === null) {
59+
$adapter = new LocalAdapter();
60+
}
61+
4962
$this->nodesFactory = $nodesFactory;
63+
$this->adapter = $adapter;
5064
}
5165

5266
/**
@@ -57,7 +71,7 @@ public function __construct(NodesFactory $nodesFactory)
5771
*/
5872
public function matches($filePath)
5973
{
60-
return is_string($filePath) && file_exists($filePath);
74+
return is_string($filePath) && $this->adapter->fileExists($filePath);
6175
}
6276

6377
/**
@@ -80,11 +94,11 @@ public function create($object, StrategyContainer $strategies, Context $context
8094
)
8195
);
8296
}
83-
$code = file_get_contents($object);
97+
$code = $this->adapter->getContents($object);
8498
$nodes = $this->nodesFactory->create($code);
8599
$docBlock = $this->createDocBlock($strategies, $code, $nodes);
86100

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

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* phpDocumentor
4+
*
5+
* PHP Version 5.5
6+
*
7+
* @copyright 2010-2015 Mike van Riel / Naenius (http://www.naenius.com)
8+
* @license http://www.opensource.org/licenses/mit-license.php MIT
9+
* @link http://phpdoc.org
10+
*/
11+
12+
namespace phpDocumentor\Reflection\Php\Factory\File;
13+
14+
/**
15+
* Interface for Adapters uses by the File strategy class to interact with the file system.
16+
*/
17+
interface Adapter
18+
{
19+
20+
/**
21+
* Returns true when the file exists.
22+
*
23+
* @param string $filePath
24+
* @return boolean
25+
*/
26+
public function fileExists($filePath);
27+
28+
/**
29+
* Returns the content of the file as a string.
30+
*
31+
* @param $filePath
32+
* @return string
33+
*/
34+
public function getContents($filePath);
35+
36+
/**
37+
* Returns md5 hash of the file.
38+
*
39+
* @param $filePath
40+
* @return string
41+
*/
42+
public function md5($filePath);
43+
44+
/**
45+
* Returns an relative path to the file.
46+
*
47+
* @param string $filePath
48+
* @return string
49+
*/
50+
public function path($filePath);
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* phpDocumentor
4+
*
5+
* PHP Version 5.5
6+
*
7+
* @copyright 2010-2015 Mike van Riel / Naenius (http://www.naenius.com)
8+
* @license http://www.opensource.org/licenses/mit-license.php MIT
9+
* @link http://phpdoc.org
10+
*/
11+
12+
namespace phpDocumentor\Reflection\Php\Factory\File;
13+
14+
/**
15+
* Adapter to interact with local readable files.
16+
*/
17+
final class LocalAdapter implements Adapter
18+
{
19+
20+
/**
21+
* Returns true when the file exists.
22+
*
23+
* @param string $filePath
24+
* @return boolean
25+
*/
26+
public function fileExists($filePath)
27+
{
28+
return file_exists($filePath);
29+
}
30+
31+
/**
32+
* Returns the content of the file as a string.
33+
*
34+
* @param $filePath
35+
* @return string
36+
*/
37+
public function getContents($filePath)
38+
{
39+
return file_get_contents($filePath);
40+
}
41+
42+
/**
43+
* Returns md5 hash of the file.
44+
*
45+
* @param $filePath
46+
* @return string
47+
*/
48+
public function md5($filePath)
49+
{
50+
return md5_file($filePath);
51+
}
52+
53+
/**
54+
* Returns an relative path to the file.
55+
*
56+
* @param string $filePath
57+
* @return string
58+
*/
59+
public function path($filePath)
60+
{
61+
return $filePath;
62+
}
63+
}

0 commit comments

Comments
 (0)