1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette\Object;
11: use Nette\Utils\Strings;
12:
13:
14: /**
15: * Instance of PHP file.
16: *
17: * Generates:
18: * - opening tag (<?php)
19: * - doc comments
20: * - one or more namespaces
21: */
22: class PhpFile extends Object
23: {
24: /** @var string[] */
25: private $documents = array();
26:
27: /** @var PhpNamespace[] */
28: private $namespaces = array();
29:
30:
31: /**
32: * @param string|NULL
33: * @return self
34: */
35: public function setComment($val)
36: {
37: $this->documents = $val ? array((string) $val) : array();
38: return $this;
39: }
40:
41:
42: /**
43: * @return string|NULL
44: */
45: public function getComment()
46: {
47: return implode($this->documents) ?: NULL;
48: }
49:
50:
51: /**
52: * @param string
53: * @return self
54: */
55: public function addComment($val)
56: {
57: return $this->addDocument($val);
58: }
59:
60:
61: /**
62: * @param string[]
63: * @return self
64: */
65: public function setDocuments(array $documents)
66: {
67: $this->documents = $documents;
68: return $this;
69: }
70:
71:
72: /**
73: * @return string[]
74: */
75: public function getDocuments()
76: {
77: return $this->documents;
78: }
79:
80:
81: /**
82: * @param string
83: * @return self
84: */
85: public function addDocument($document)
86: {
87: $this->documents[] = $document;
88: return $this;
89: }
90:
91:
92: /**
93: * @param string
94: * @return ClassType
95: */
96: public function addClass($name)
97: {
98: return $this
99: ->addNamespace(Helpers::extractNamespace($name))
100: ->addClass(Helpers::extractShortName($name));
101: }
102:
103:
104: /**
105: * @param string
106: * @return ClassType
107: */
108: public function addInterface($name)
109: {
110: return $this
111: ->addNamespace(Helpers::extractNamespace($name))
112: ->addInterface(Helpers::extractShortName($name));
113: }
114:
115:
116: /**
117: * @param string
118: * @return ClassType
119: */
120: public function addTrait($name)
121: {
122: return $this
123: ->addNamespace(Helpers::extractNamespace($name))
124: ->addTrait(Helpers::extractShortName($name));
125: }
126:
127:
128: /**
129: * @param string NULL means global namespace
130: * @return PhpNamespace
131: */
132: public function addNamespace($name)
133: {
134: if (!isset($this->namespaces[$name])) {
135: $this->namespaces[$name] = new PhpNamespace($name);
136: }
137: return $this->namespaces[$name];
138: }
139:
140:
141: /**
142: * @return string PHP code
143: */
144: public function __toString()
145: {
146: foreach ($this->namespaces as $namespace) {
147: $namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces[NULL]));
148: }
149:
150: return Strings::normalize(
151: "<?php\n"
152: . ($this->documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n\n" : '')
153: . implode("\n\n", $this->namespaces)
154: ) . "\n";
155: }
156:
157: }
158: