ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 = [];
28 
32  protected $application;
33 
37  public function __construct(array $frame)
38  {
39  $this->frame = $frame;
40  }
41 
46  public function getFile($shortened = false)
47  {
48  if (empty($this->frame['file'])) {
49  return null;
50  }
51 
52  $file = $this->frame['file'];
53 
54  // Check if this frame occurred within an eval().
55  // @todo: This can be made more reliable by checking if we've entered
56  // eval() in a previous trace, but will need some more work on the upper
57  // trace collector(s).
58  if (preg_match('/^(.*)\((\d+)\) : (?:eval\(\)\'d|assert) code$/', $file, $matches)) {
59  $file = $this->frame['file'] = $matches[1];
60  $this->frame['line'] = (int) $matches[2];
61  }
62 
63  if ($shortened && is_string($file)) {
64  // Replace the part of the path that all frames have in common, and add 'soft hyphens' for smoother line-breaks.
65  $dirname = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
66  if ($dirname !== '/') {
67  $file = str_replace($dirname, "&hellip;", $file);
68  }
69  $file = str_replace("/", "/&shy;", $file);
70  }
71 
72  return $file;
73  }
74 
78  public function getLine()
79  {
80  return isset($this->frame['line']) ? $this->frame['line'] : null;
81  }
82 
86  public function getClass()
87  {
88  return isset($this->frame['class']) ? $this->frame['class'] : null;
89  }
90 
94  public function getFunction()
95  {
96  return isset($this->frame['function']) ? $this->frame['function'] : null;
97  }
98 
102  public function getArgs()
103  {
104  return isset($this->frame['args']) ? (array) $this->frame['args'] : [];
105  }
106 
112  public function getFileContents()
113  {
114  if ($this->fileContentsCache === null && $filePath = $this->getFile()) {
115  // Leave the stage early when 'Unknown' is passed
116  // this would otherwise raise an exception when
117  // open_basedir is enabled.
118  if ($filePath === "Unknown") {
119  return null;
120  }
121 
122  // Return null if the file doesn't actually exist.
123  if (!is_file($filePath)) {
124  return null;
125  }
126 
127  $this->fileContentsCache = file_get_contents($filePath);
128  }
129 
131  }
132 
144  public function addComment($comment, $context = 'global')
145  {
146  $this->comments[] = [
147  'comment' => $comment,
148  'context' => $context,
149  ];
150  }
151 
160  public function getComments($filter = null)
161  {
163 
164  if ($filter !== null) {
165  $comments = array_filter($comments, function ($c) use ($filter) {
166  return $c['context'] == $filter;
167  });
168  }
169 
170  return $comments;
171  }
172 
179  public function getRawFrame()
180  {
181  return $this->frame;
182  }
183 
202  public function getFileLines($start = 0, $length = null)
203  {
204  if (null !== ($contents = $this->getFileContents())) {
205  $lines = explode("\n", $contents);
206 
207  // Get a subset of lines from $start to $end
208  if ($length !== null) {
209  $start = (int) $start;
210  $length = (int) $length;
211  if ($start < 0) {
212  $start = 0;
213  }
214 
215  if ($length <= 0) {
216  throw new InvalidArgumentException(
217  "\$length($length) cannot be lower or equal to 0"
218  );
219  }
220 
221  $lines = array_slice($lines, $start, $length, true);
222  }
223 
224  return $lines;
225  }
226  }
227 
235  public function serialize()
236  {
238  if (!empty($this->comments)) {
239  $frame['_comments'] = $this->comments;
240  }
241 
242  return serialize($frame);
243  }
244 
252  public function unserialize($serializedFrame)
253  {
254  $frame = unserialize($serializedFrame);
255 
256  if (!empty($frame['_comments'])) {
257  $this->comments = $frame['_comments'];
258  unset($frame['_comments']);
259  }
260 
261  $this->frame = $frame;
262  }
263 
269  public function equals(Frame $frame)
270  {
271  if (!$this->getFile() || $this->getFile() === 'Unknown' || !$this->getLine()) {
272  return false;
273  }
274  return $frame->getFile() === $this->getFile() && $frame->getLine() === $this->getLine();
275  }
276 
282  public function isApplication()
283  {
284  return $this->application;
285  }
286 
292  public function setApplication($application)
293  {
294  $this->application = $application;
295  }
296 }
serialize()
Implements the Serializable interface, with special steps to also save the existing comments...
Definition: Frame.php:235
getFile($shortened=false)
Definition: Frame.php:46
setApplication($application)
Mark as an frame belonging to the application.
Definition: Frame.php:292
getRawFrame()
Returns the array containing the raw frame data from which this Frame object was built.
Definition: Frame.php:179
equals(Frame $frame)
Compares Frame against one another.
Definition: Frame.php:269
addComment($comment, $context='global')
Adds a comment to this frame, that can be received and used by other handlers.
Definition: Frame.php:144
__construct(array $frame)
Definition: Frame.php:37
Whoops - php errors for cool kids.
isApplication()
Returns whether this frame belongs to the application or not.
Definition: Frame.php:282
$comment
Definition: buildRTE.php:83
Create styles array
The data for the language used.
getFileLines($start=0, $length=null)
Definition: Frame.php:202
getComments($filter=null)
Returns all comments for this frame.
Definition: Frame.php:160
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
unserialize($serializedFrame)
Unserializes the frame data, while also preserving any existing comment data.
Definition: Frame.php:252
Add comments
getFileContents()
Returns the full contents of the file for this frame, if it&#39;s known.
Definition: Frame.php:112