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