Skip to content

Commit

Permalink
Merge pull request #715 from sergeyklay/feature/extend/annotations
Browse files Browse the repository at this point in the history
Added Phalcon\Annotations\Extended\Adapter
  • Loading branch information
sergeyklay authored Dec 15, 2016
2 parents a553d80 + b0fb51c commit 1fa47bc
Show file tree
Hide file tree
Showing 17 changed files with 1,271 additions and 7 deletions.
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,7 @@ install:
- ln -s ${TRAVIS_BUILD_DIR}/.temp ${TRAVIS_BUILD_DIR}/cphalcon/.temp
- ( cd cphalcon; zephir fullclean && zephir generate $ZEND_BACKEND )
- ( cd cphalcon/ext; export CFLAGS="-g3 -O1 -std=gnu90 -Wall -DZEPHIR_RELEASE=1"; /usr/bin/phpize &> /dev/null && ./configure --silent --enable-phalcon &> /dev/null && make --silent -j3 &> /dev/null && make --silent install )
- phpenv config-add tests/_ci/phalcon.ini
- phpenv config-add tests/_ci/redis.ini
- if [[ "${PHP_MAJOR:0:1}" != "7" ]]; then phpenv config-add tests/_ci/mongo.ini; fi;
- phpenv config-add tests/_ci/mongodb.ini
- phpenv config-add tests/_ci/memcached.ini
- pecl channel-update pecl.php.net
- if [[ "${PHP_MAJOR:0:1}" = "7" ]]; then printf "\n" | pecl install yaml-2.0.0 > /dev/null 2>&1; else printf "\n" | pecl install yaml > /dev/null 2>&1; fi;
- ( bash tests/_ci/install_prereqs_$PHP_MAJOR.sh )

before_script:
- stty cols 160
Expand Down
92 changes: 92 additions & 0 deletions Library/Phalcon/Annotations/Extended/AbstractAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Annotations\Extended;

use Phalcon\Annotations\Adapter;
use Phalcon\Annotations\Exception;
use Phalcon\Annotations\Reflection;
use Phalcon\Traits\ConfigurableTrait;

/**
* Phalcon\Annotations\Extended\AbstractAdapter
*
* This is the base class for Phalcon\Annotations\Extended adapters
*
* @package Phalcon\Annotations\Extended
*/
abstract class AbstractAdapter extends Adapter implements AdapterInterface
{
use ConfigurableTrait;

/**
* Configurable properties.
* @var array
*/
protected $configurable = [];

/**
* AbstractAdapter constructor.
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->setParameters($options);
}

/**
* Returns prefixed identifier.
*
* @param string $id
* @return string
*/
abstract protected function getPrefixedIdentifier($id);

/**
* Check and cast returned result.
*
* @param mixed $result
* @return bool
*/
protected function castResult($result)
{
if ($result instanceof Reflection) {
return $result;
}

return false;
}

/**
* Check annotation key.
*
* @param string $key
*
* @throws Exception
*/
protected function checkKey($key)
{
if (!is_string($key)) {
throw new Exception(
sprintf('Invalid key type key to retrieve annotations. Expected string but got %s.', gettype($key))
);
}
}
}
201 changes: 201 additions & 0 deletions Library/Phalcon/Annotations/Extended/Adapter/Apc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Annotations\Extended\Adapter;

use Phalcon\Annotations\Exception;
use Phalcon\Annotations\Reflection;
use Phalcon\Annotations\Extended\AbstractAdapter;

/**
* Phalcon\Annotations\Extended\Adapter\Apc
*
* Extended Apc adapter for storing annotations in the APC(u).
* This adapter is suitable for production.
*
* <code>
* use Phalcon\Annotations\Extended\Adapter\Apc;
*
* $annotations = new Apc(
* [
* 'prefix' => 'app-annotations', // Optional
* 'lifetime' => 8600, // Optional
* 'statsKey' => '_PHAN', // Optional
* ]
* );
* </code>
*
* @package Phalcon\Annotations\Extended\Adapter
*/
class Apc extends AbstractAdapter
{
/**
* The storage key prefix.
* @var string
*/
protected $prefix = '';

/**
* The storage lifetime.
* @var int
*/
protected $lifetime = 8600;

/**
* Storage stats key
* @var string
*/
protected $statsKey = '_PHAN';

/**
* Configurable properties.
* @var array
*/
protected $configurable = [
'prefix',
'lifetime',
'statsKey',
];

/**
* Reads parsed annotations from APC(u).
*
* @param string $key
* @return Reflection|bool
*
* @throws Exception
*/
public function read($key)
{
$this->checkKey($key);

$prefixedKey = $this->getPrefixedIdentifier($key);

if (function_exists('apcu_fetch')) {
$result = apcu_fetch($prefixedKey);
} else {
$result = apc_fetch($prefixedKey);
}

return $this->castResult($result);
}

/**
* Writes parsed annotations to APC(u)
*
* @param string $key
* @param Reflection $data
* @return bool
*
* @throws Exception
*/
public function write($key, Reflection $data)
{
$this->checkKey($key);

$prefixedKey = $this->getPrefixedIdentifier($key);

if (function_exists('apcu_store')) {
return apcu_store($prefixedKey, $data, $this->lifetime);
}

return apc_store($prefixedKey, $data, $this->lifetime);
}

/**
* {@inheritdoc}
*
* <code>
* use Phalcon\Annotations\Extended\Apc;
*
* $annotations = new Apc(['prefix' => 'app-annotations']);
* $annotations->flush();
* </code>
*
* @return bool
*/
public function flush()
{
$prefixPattern = '#^_PHAN' . preg_quote("{$this->prefix}", '#') . '#';

if (class_exists('\APCuIterator')) {
foreach (new \APCuIterator($prefixPattern) as $item) {
apcu_delete($item['key']);
}

return true;
}

foreach (new \APCIterator('user', $prefixPattern) as $item) {
apc_delete($item['key']);
}

return true;
}

/**
* {@inheritdoc}
*
* @param string $id
* @return string
*/
protected function getPrefixedIdentifier($id)
{
return $this->statsKey . $this->prefix . $id;
}

/**
* Sets the storage key prefix.
*
* @param string $prefix The storage key prefix.
* @return $this
*/
protected function setPrefix($prefix)
{
$this->prefix = (string) $prefix;

return $this;
}

/**
* Sets the storage lifetime.
*
* @param int $lifetime The storage lifetime.
* @return $this
*/
protected function setLifetime($lifetime)
{
$this->lifetime = (int) $lifetime;

return $this;
}

/**
* Sets the storage stats key.
*
* @param string $statsKey The storage key prefix.
* @return $this
*/
protected function setStatsKey($statsKey)
{
$this->statsKey = (string) $statsKey;

return $this;
}
}
Loading

0 comments on commit 1fa47bc

Please sign in to comment.