Mark V
1: <?php
2: namespace MOC\V\Core\FileSystem\Component\Bridge\Repository;
3:
4: use MOC\V\Core\AutoLoader\AutoLoader;
5: use MOC\V\Core\FileSystem\Component\Bridge\Bridge;
6: use MOC\V\Core\FileSystem\Component\IBridgeInterface;
7: use MOC\V\Core\FileSystem\Component\Parameter\Repository\FileParameter;
8: use Symfony\Component\Finder\Finder;
9: use Symfony\Component\Finder\SplFileInfo;
10:
11: /**
12: * Class SymfonyFinder
13: *
14: * @package MOC\V\Core\FileSystem\Component\Bridge\Repository
15: */
16: class SymfonyFinder extends Bridge implements IBridgeInterface
17: {
18:
19: /** @var Finder $Instance */
20: private $Instance = null;
21: /** @var FileParameter|null $FileOption */
22: private $FileOption = null;
23:
24: /**
25: * @param FileParameter $FileOption
26: */
27: public function __construct(FileParameter $FileOption)
28: {
29:
30: parent::__construct();
31: AutoLoader::getNamespaceAutoLoader('Symfony\Component', __DIR__.'/../../../Vendor/');
32:
33: $this->FileOption = $FileOption;
34:
35: try {
36: $this->Instance = new Finder();
37: $this->Instance->useBestAdapter()->files()
38: ->name(pathinfo($FileOption->getFile(), PATHINFO_BASENAME))
39: ->in(pathinfo($FileOption->getFile(), PATHINFO_DIRNAME));
40: } catch (\Exception $Exception) {
41: // Nothing
42: }
43: }
44:
45: /**
46: * @return string
47: */
48: public function __toString()
49: {
50:
51: if ($this->getRealPath()) {
52: return (string)file_get_contents($this->getRealPath());
53: } else {
54: return '';
55: }
56: }
57:
58: /**
59: * @return string
60: */
61: public function getRealPath()
62: {
63:
64: try {
65: $Result = array();
66: /** @var SplFileInfo $File */
67: foreach ($this->Instance as $File) {
68: array_push($Result, $File->getRealPath());
69: }
70:
71: if (count($Result) > 1) {
72: throw new \Exception(count($Result).' matches.');
73: } elseif (count($Result) == 1) {
74: return current($Result);
75: } else {
76: return '';
77: }
78: } catch (\Exception $Exception) {
79: return '';
80: }
81: }
82:
83: /**
84: * @return string
85: */
86: public function getLocation()
87: {
88:
89: return $this->FileOption->getFile();
90: }
91: }
92: