PluginProbe
Packeta / 1.6.1
Packeta v1.6.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / php-generator / src / PhpGenerator / PhpFile.php

PhpFile.php in Packeta 1.6.1, at deps/nette/php-generator/src/PhpGenerator/PhpFile.php

96 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette\PhpGenerator;
9
10 use Packetery\Nette;
11 /**
12 * Instance of PHP file.
13 *
14 * Generates:
15 * - opening tag (<?php)
16 * - doc comments
17 * - one or more namespaces
18 */
19 final class PhpFile
20 {
21 use \Packetery\Nette\SmartObject;
22 use Traits\CommentAware;
23 /** @var PhpNamespace[] */
24 private $namespaces = [];
25 /** @var bool */
26 private $strictTypes = \false;
27 public function addClass(string $name) : ClassType
28 {
29 return $this->addNamespace(Helpers::extractNamespace($name))->addClass(Helpers::extractShortName($name));
30 }
31 public function addInterface(string $name) : ClassType
32 {
33 return $this->addNamespace(Helpers::extractNamespace($name))->addInterface(Helpers::extractShortName($name));
34 }
35 public function addTrait(string $name) : ClassType
36 {
37 return $this->addNamespace(Helpers::extractNamespace($name))->addTrait(Helpers::extractShortName($name));
38 }
39 /** @param string|PhpNamespace $namespace */
40 public function addNamespace($namespace) : PhpNamespace
41 {
42 if ($namespace instanceof PhpNamespace) {
43 $res = $this->namespaces[$namespace->getName()] = $namespace;
44 } elseif (\is_string($namespace)) {
45 $res = $this->namespaces[$namespace] = $this->namespaces[$namespace] ?? new PhpNamespace($namespace);
46 } else {
47 throw new \Packetery\Nette\InvalidArgumentException('Argument must be string|PhpNamespace.');
48 }
49 foreach ($this->namespaces as $namespace) {
50 $namespace->setBracketedSyntax(\count($this->namespaces) > 1 && isset($this->namespaces['']));
51 }
52 return $res;
53 }
54 /** @return PhpNamespace[] */
55 public function getNamespaces() : array
56 {
57 return $this->namespaces;
58 }
59 /** @return static */
60 public function addUse(string $name, string $alias = null) : self
61 {
62 $this->addNamespace('')->addUse($name, $alias);
63 return $this;
64 }
65 /**
66 * Adds declare(strict_types=1) to output.
67 * @return static
68 */
69 public function setStrictTypes(bool $on = \true) : self
70 {
71 $this->strictTypes = $on;
72 return $this;
73 }
74 public function hasStrictTypes() : bool
75 {
76 return $this->strictTypes;
77 }
78 /** @deprecated use hasStrictTypes() */
79 public function getStrictTypes() : bool
80 {
81 return $this->strictTypes;
82 }
83 public function __toString() : string
84 {
85 try {
86 return (new Printer())->printFile($this);
87 } catch (\Throwable $e) {
88 if (\PHP_VERSION_ID >= 70400) {
89 throw $e;
90 }
91 \trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", \E_USER_ERROR);
92 return '';
93 }
94 }
95 }
96