1 | <?php |
||
25 | final class ProjectFactory implements ProjectFactoryInterface |
||
|
|||
26 | { |
||
27 | /** |
||
28 | * @var ProjectFactoryStrategies[] |
||
29 | */ |
||
30 | private $strategies; |
||
31 | |||
32 | /** |
||
33 | * Initializes the factory with a number of strategies. |
||
34 | * |
||
35 | * @param ProjectFactoryStrategy[] $strategies |
||
36 | */ |
||
37 | public function __construct($strategies) |
||
38 | { |
||
39 | $this->strategies = new ProjectFactoryStrategies($strategies); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Creates a new instance of this factory. With all default strategies. |
||
44 | * |
||
45 | * @return static; |
||
46 | */ |
||
47 | public static function createInstance() |
||
48 | { |
||
49 | return new static( |
||
50 | [ |
||
51 | new Factory\Argument(new PrettyPrinter()), |
||
52 | new Factory\Class_(), |
||
53 | new Factory\Constant(new PrettyPrinter()), |
||
54 | new Factory\DocBlock(DocBlockFactory::createInstance()), |
||
55 | new Factory\File(NodesFactory::createInstance()), |
||
56 | new Factory\Function_(), |
||
57 | new Factory\Interface_(), |
||
58 | new Factory\Method(), |
||
59 | new Factory\Property(new PrettyPrinter()), |
||
60 | new Factory\Trait_(), |
||
61 | ] |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Creates a project from the set of files. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * @param \phpDocumentor\Reflection\File[] $files |
||
70 | * @return Project |
||
71 | * @throws Exception when no matching strategy was found. |
||
72 | */ |
||
73 | 9 | public function create($name, array $files) |
|
74 | { |
||
75 | 9 | $project = new Project($name); |
|
76 | |||
77 | 9 | foreach ($files as $filePath) { |
|
78 | 9 | $strategy = $this->strategies->findMatching($filePath); |
|
79 | 8 | $file = $strategy->create($filePath, $this->strategies); |
|
80 | 8 | if ($file !== null) { |
|
81 | 8 | $project->addFile($file); |
|
82 | } |
||
83 | } |
||
84 | |||
85 | 8 | $this->buildNamespaces($project); |
|
86 | |||
87 | 8 | return $project; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Builds the namespace tree with all elements in the project. |
||
92 | * |
||
93 | * @param Project $project |
||
94 | */ |
||
95 | 8 | private function buildNamespaces(Project $project) |
|
104 | |||
105 | /** |
||
106 | * Gets Namespace from the project if it exists, otherwise returns a new namepace |
||
107 | * |
||
108 | * @param Project $project |
||
109 | * @param $name |
||
110 | * @return Namespace_ |
||
111 | */ |
||
112 | 7 | private function getNamespaceByName(Project $project, $name) |
|
124 | |||
125 | /** |
||
126 | * Adds all elements belonging to the namespace to the namespace. |
||
127 | * |
||
128 | * @param File $file |
||
129 | * @param Namespace_ $namespace |
||
130 | */ |
||
131 | 7 | private function buildNamespace(File $file, Namespace_ $namespace) |
|
163 | } |
||
164 |