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;
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
23: {
24: use Nette\SmartObject;
25:
26: /** @var string|NULL */
27: private $comment;
28:
29: /** @var PhpNamespace[] */
30: private $namespaces = [];
31:
32:
33: /**
34: * @param string|NULL
35: * @return static
36: */
37: public function setComment($val)
38: {
39: $this->comment = $val ? (string) $val : NULL;
40: return $this;
41: }
42:
43:
44: /**
45: * @return string|NULL
46: */
47: public function getComment()
48: {
49: return $this->comment;
50: }
51:
52:
53: /**
54: * @param string
55: * @return static
56: */
57: public function addComment($val)
58: {
59: $this->comment .= $this->comment ? "\n$val" : $val;
60: return $this;
61: }
62:
63:
64: /** @deprecated */
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: /** @deprecated */
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: /** @deprecated */
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: * @param string
90: * @return ClassType
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: * @param string
102: * @return ClassType
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: * @param string
114: * @return ClassType
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: * @param string NULL means global namespace
126: * @return PhpNamespace
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: * @return string PHP code
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" . Helpers::formatDocComment($this->comment . "\n") . "\n" : '')
149: . implode("\n\n", $this->namespaces)
150: ) . "\n";
151: }
152:
153: }
154: