ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Frame.php
Go to the documentation of this file.
1 <?php
7 namespace Whoops\Exception;
8 
10 use Serializable;
11 
12 class Frame implements Serializable
13 {
17  protected $frame;
18 
22  protected $fileContentsCache;
23 
27  protected $comments = array();
28 
32  public function __construct(array $frame)
33  {
34  $this->frame = $frame;
35  }
36 
41  public function getFile($shortened = false)
42  {
43  if (empty($this->frame['file'])) {
44  return null;
45  }
46 
47  $file = $this->frame['file'];
48 
49  // Check if this frame occurred within an eval().
50  // @todo: This can be made more reliable by checking if we've entered
51  // eval() in a previous trace, but will need some more work on the upper
52  // trace collector(s).
53  if (preg_match('/^(.*)\((\d+)\) : (?:eval\(\)\'d|assert) code$/', $file, $matches)) {
54  $file = $this->frame['file'] = $matches[1];
55  $this->frame['line'] = (int) $matches[2];
56  }
57 
58  if ($shortened && is_string($file)) {
59  // Replace the part of the path that all frames have in common, and add 'soft hyphens' for smoother line-breaks.
60  $dirname = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
61  $file = str_replace($dirname, "…", $file);
62  $file = str_replace("/", "/&shy;", $file);
63  }
64 
65  return $file;
66  }
67 
71  public function getLine()
72  {
73  return isset($this->frame['line']) ? $this->frame['line'] : null;
74  }
75 
79  public function getClass()
80  {
81  return isset($this->frame['class']) ? $this->frame['class'] : null;
82  }
83 
87  public function getFunction()
88  {
89  return isset($this->frame['function']) ? $this->frame['function'] : null;
90  }
91 
95  public function getArgs()
96  {
97  return isset($this->frame['args']) ? (array) $this->frame['args'] : array();
98  }
99 
105  public function getFileContents()
106  {
107  if ($this->fileContentsCache === null && $filePath = $this->getFile()) {
108  // Leave the stage early when 'Unknown' is passed
109  // this would otherwise raise an exception when
110  // open_basedir is enabled.
111  if ($filePath === "Unknown") {
112  return null;
113  }
114 
115  // Return null if the file doesn't actually exist.
116  if (!is_file($filePath)) {
117  return null;
118  }
119 
120  $this->fileContentsCache = file_get_contents($filePath);
121  }
122 
124  }
125 
137  public function addComment($comment, $context = 'global')
138  {
139  $this->comments[] = array(
140  'comment' => $comment,
141  'context' => $context,
142  );
143  }
144 
153  public function getComments($filter = null)
154  {
156 
157  if ($filter !== null) {
158  $comments = array_filter($comments, function ($c) use ($filter) {
159  return $c['context'] == $filter;
160  });
161  }
162 
163  return $comments;
164  }
165 
172  public function getRawFrame()
173  {
174  return $this->frame;
175  }
176 
195  public function getFileLines($start = 0, $length = null)
196  {
197  if (null !== ($contents = $this->getFileContents())) {
198  $lines = explode("\n", $contents);
199 
200  // Get a subset of lines from $start to $end
201  if ($length !== null) {
202  $start = (int) $start;
203  $length = (int) $length;
204  if ($start < 0) {
205  $start = 0;
206  }
207 
208  if ($length <= 0) {
209  throw new InvalidArgumentException(
210  "\$length($length) cannot be lower or equal to 0"
211  );
212  }
213 
214  $lines = array_slice($lines, $start, $length, true);
215  }
216 
217  return $lines;
218  }
219  }
220 
228  public function serialize()
229  {
231  if (!empty($this->comments)) {
232  $frame['_comments'] = $this->comments;
233  }
234 
235  return serialize($frame);
236  }
237 
245  public function unserialize($serializedFrame)
246  {
247  $frame = unserialize($serializedFrame);
248 
249  if (!empty($frame['_comments'])) {
250  $this->comments = $frame['_comments'];
251  unset($frame['_comments']);
252  }
253 
254  $this->frame = $frame;
255  }
256 
262  public function equals(Frame $frame)
263  {
264  if (!$this->getFile() || $this->getFile() === 'Unknown' || !$this->getLine()) {
265  return false;
266  }
267  return $frame->getFile() === $this->getFile() && $frame->getLine() === $this->getLine();
268  }
269 }
serialize()
Implements the Serializable interface, with special steps to also save the existing comments...
Definition: Frame.php:228
print $file
getFile($shortened=false)
Definition: Frame.php:41
getRawFrame()
Returns the array containing the raw frame data from which this Frame object was built.
Definition: Frame.php:172
equals(Frame $frame)
Compares Frame against one another.
Definition: Frame.php:262
addComment($comment, $context='global')
Adds a comment to this frame, that can be received and used by other handlers.
Definition: Frame.php:137
__construct(array $frame)
Definition: Frame.php:32
Whoops - php errors for cool kids.
$comment
Definition: buildRTE.php:83
getFileLines($start=0, $length=null)
Definition: Frame.php:195
getComments($filter=null)
Returns all comments for this frame.
Definition: Frame.php:153
unserialize($serializedFrame)
Unserializes the frame data, while also preserving any existing comment data.
Definition: Frame.php:245
getFileContents()
Returns the full contents of the file for this frame, if it&#39;s known.
Definition: Frame.php:105