Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

BaseMemcacheProfilerStorage.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 7.49 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpKernel\Profiler;
013   
014  /**
015   * Base Memcache storage for profiling information in a Memcache.
016   *
017   * @author Andrej Hudec <pulzarraider@gmail.com>
018   */
019  abstract class BaseMemcacheProfilerStorage implements ProfilerStorageInterface
020  {
021      const TOKEN_PREFIX = 'sf_profiler_';
022   
023      protected $dsn;
024      protected $lifetime;
025   
026      /**
027       * Constructor.
028       *
029       * @param string $dsn      A data source name
030       * @param string $username
031       * @param string $password
032       * @param int    $lifetime The lifetime to use for the purge
033       */
034      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
035      {
036          $this->dsn = $dsn;
037          $this->lifetime = (int) $lifetime;
038      }
039   
040      /**
041       * {@inheritdoc}
042       */
043      public function find($ip, $url, $limit, $method, $start = null, $end = null)
044      {
045          $indexName = $this->getIndexName();
046   
047          $indexContent = $this->getValue($indexName);
048          if (!$indexContent) {
049              return array();
050          }
051   
052          $profileList = explode("\n", $indexContent);
053          $result = array();
054   
055          foreach ($profileList as $item) {
056              if ($limit === 0) {
057                  break;
058              }
059   
060              if ($item == '') {
061                  continue;
062              }
063   
064              list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
065   
066              $itemTime = (int) $itemTime;
067   
068              if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method)) {
069                  continue;
070              }
071   
072              if (!empty($start) && $itemTime < $start) {
073                  continue;
074              }
075   
076              if (!empty($end) && $itemTime > $end) {
077                  continue;
078              }
079   
080              $result[$itemToken]  = array(
081                  'token'  => $itemToken,
082                  'ip'     => $itemIp,
083                  'method' => $itemMethod,
084                  'url'    => $itemUrl,
085                  'time'   => $itemTime,
086                  'parent' => $itemParent,
087              );
088              --$limit;
089          }
090   
091          usort($result, function ($a, $b) {
092              if ($a['time'] === $b['time']) {
093                  return 0;
094              }
095   
096              return $a['time'] > $b['time'] ? -1 : 1;
097          });
098   
099          return $result;
100      }
101   
102      /**
103       * {@inheritdoc}
104       */
105      public function purge()
106      {
107          // delete only items from index
108          $indexName = $this->getIndexName();
109   
110          $indexContent = $this->getValue($indexName);
111   
112          if (!$indexContent) {
113              return false;
114          }
115   
116          $profileList = explode("\n", $indexContent);
117   
118          foreach ($profileList as $item) {
119              if ($item == '') {
120                  continue;
121              }
122   
123              if (false !== $pos = strpos($item, "\t")) {
124                  $this->delete($this->getItemName(substr($item, 0, $pos)));
125              }
126          }
127   
128          return $this->delete($indexName);
129      }
130   
131      /**
132       * {@inheritdoc}
133       */
134      public function read($token)
135      {
136          if (empty($token)) {
137              return false;
138          }
139   
140          $profile = $this->getValue($this->getItemName($token));
141   
142          if (false !== $profile) {
143              $profile = $this->createProfileFromData($token, $profile);
144          }
145   
146          return $profile;
147      }
148   
149      /**
150       * {@inheritdoc}
151       */
152      public function write(Profile $profile)
153      {
154          $data = array(
155              'token'    => $profile->getToken(),
156              'parent'   => $profile->getParentToken(),
157              'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
158              'data'     => $profile->getCollectors(),
159              'ip'       => $profile->getIp(),
160              'method'   => $profile->getMethod(),
161              'url'      => $profile->getUrl(),
162              'time'     => $profile->getTime(),
163          );
164   
165          $profileIndexed = false !== $this->getValue($this->getItemName($profile->getToken()));
166   
167          if ($this->setValue($this->getItemName($profile->getToken()), $data, $this->lifetime)) {
168              if (!$profileIndexed) {
169                  // Add to index
170                  $indexName = $this->getIndexName();
171   
172                  $indexRow = implode("\t", array(
173                      $profile->getToken(),
174                      $profile->getIp(),
175                      $profile->getMethod(),
176                      $profile->getUrl(),
177                      $profile->getTime(),
178                      $profile->getParentToken(),
179                  ))."\n";
180   
181                  return $this->appendValue($indexName, $indexRow, $this->lifetime);
182              }
183   
184              return true;
185          }
186   
187          return false;
188      }
189   
190      /**
191       * Retrieve item from the memcache server
192       *
193       * @param string $key
194       *
195       * @return mixed
196       */
197      abstract protected function getValue($key);
198   
199      /**
200       * Store an item on the memcache server under the specified key
201       *
202       * @param string $key
203       * @param mixed  $value
204       * @param int    $expiration
205       *
206       * @return bool
207       */
208      abstract protected function setValue($key, $value, $expiration = 0);
209   
210      /**
211       * Delete item from the memcache server
212       *
213       * @param string $key
214       *
215       * @return bool
216       */
217      abstract protected function delete($key);
218   
219      /**
220       * Append data to an existing item on the memcache server
221       * @param string $key
222       * @param string $value
223       * @param int    $expiration
224       *
225       * @return bool
226       */
227      abstract protected function appendValue($key, $value, $expiration = 0);
228   
229      private function createProfileFromData($token, $data, $parent = null)
230      {
231          $profile = new Profile($token);
232          $profile->setIp($data['ip']);
233          $profile->setMethod($data['method']);
234          $profile->setUrl($data['url']);
235          $profile->setTime($data['time']);
236          $profile->setCollectors($data['data']);
237   
238          if (!$parent && $data['parent']) {
239              $parent = $this->read($data['parent']);
240          }
241   
242          if ($parent) {
243              $profile->setParent($parent);
244          }
245   
246          foreach ($data['children'] as $token) {
247              if (!$token) {
248                  continue;
249              }
250   
251              if (!$childProfileData = $this->getValue($this->getItemName($token))) {
252                  continue;
253              }
254   
255              $profile->addChild($this->createProfileFromData($token, $childProfileData, $profile));
256          }
257   
258          return $profile;
259      }
260   
261      /**
262       * Get item name
263       *
264       * @param string $token
265       *
266       * @return string
267       */
268      private function getItemName($token)
269      {
270          $name = self::TOKEN_PREFIX.$token;
271   
272          if ($this->isItemNameValid($name)) {
273              return $name;
274          }
275   
276          return false;
277      }
278   
279      /**
280       * Get name of index
281       *
282       * @return string
283       */
284      private function getIndexName()
285      {
286          $name = self::TOKEN_PREFIX.'index';
287   
288          if ($this->isItemNameValid($name)) {
289              return $name;
290          }
291   
292          return false;
293      }
294   
295      private function isItemNameValid($name)
296      {
297          $length = strlen($name);
298   
299          if ($length > 250) {
300              throw new \RuntimeException(sprintf('The memcache item key "%s" is too long (%s bytes). Allowed maximum size is 250 bytes.', $name, $length));
301          }
302   
303          return true;
304      }
305  }
306