Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.CrontabSectionElement.php
Go to the documentation of this file.
1 <?php
2 namespace Dcp;
3 
5 {
6  public $contextRoot;
7  public $file;
8  /**
9  * CrontabParserSectionElement constructor.
10  *
11  * @param string $contextRoot
12  * @param string $file
13  */
14  public function __construct($contextRoot, $file)
15  {
16  $this->contextRoot = $this->normalizeContextRoot($contextRoot);
17  $this->file = $this->normalizeFile($file);
18  }
19  /**
20  * Cleanup path names
21  *
22  * @param string $path
23  * @return string
24  */
25  public function normalizeFile($path)
26  {
27  /* Remove trailing slashes */
28  $path = rtrim($path, '/');
29  /* Replace multiples slashes with single slash */
30  $path = preg_replace(':/+:', '/', $path);
31  /* Remove leading relative './' notation */
32  $path = preg_replace(':^\./:', '', $path);
33  return $path;
34  }
35  /**
36  * Cleanup path names and try to resolve absolute path
37  *
38  * @param string $path
39  * @return string
40  */
41  public function normalizeContextRoot($path)
42  {
43  $path = $this->normalizeFile($path);
44  /* Try to resolve "real" path (to get rid of '../' elements) */
45  if (is_dir($path) && ($realPath = realpath($path)) !== false) {
46  $path = $realPath;
47  }
48  return $path;
49  }
50  /**
51  * Check if the section match the given context
52  *
53  * @param string $contextRoot The context's root directory
54  * @return bool
55  */
56  public function matchContextRoot($contextRoot)
57  {
58  return ($this->contextRoot === $this->normalizeContextRoot($contextRoot));
59  }
60  /**
61  * Check if the section match the given file
62  *
63  * @param string $file The cron file relative to the context's root directory
64  * @return bool
65  */
66  public function matchFile($file)
67  {
68  return ($this->file === $this->normalizeFile($file));
69  }
70  /**
71  * Check if the section match the given context and file
72  *
73  * @param string $contextRoot The context's root directory
74  * @param string $file The cron file relative to the context's root directory
75  * @return bool
76  */
77  public function match($contextRoot, $file)
78  {
79  return ($this->matchContextRoot($contextRoot) && $this->matchFile($file));
80  }
81  /**
82  * Serialize element to string
83  *
84  * @return string
85  */
86  public function __toString()
87  {
88  $lines = array();
89  $lines[] = sprintf("# BEGIN:FREEDOM_CRONTAB:%s:%s", $this->contextRoot, $this->file);
90  foreach ($this->childs as & $child) {
91  $lines[] = (string)$child;
92  }
93  $lines[] = sprintf("# END:FREEDOM_CRONTAB:%s:%s", $this->contextRoot, $this->file);
94  return implode("\n", $lines);
95  }
96 }
$file
$path
Definition: dav.php:39
← centre documentaire © anakeen