作者:rajanlami
项目:IntTes
protected function checkPreEventCanChangeArguments($method, array $args, array $expectedArgs)
{
$internalMethod = 'internal' . ucfirst($method);
$eventName = $method . '.pre';
// init mock
$this->_storage = $this->getMockForAbstractAdapter(array($internalMethod));
$this->_storage->getEventManager()->attach($eventName, function ($event) use($expectedArgs) {
$params = $event->getParams();
foreach ($expectedArgs as $k => $v) {
$params[$k] = $v;
}
});
// set expected arguments of internal method call
$tmp = $this->_storage->expects($this->once())->method($internalMethod);
$equals = array();
foreach ($expectedArgs as $v) {
$equals[] = $this->equalTo($v);
}
call_user_func_array(array($tmp, 'with'), $equals);
// run
call_user_func_array(array($this->_storage, $method), $args);
}
作者:parrotcag
项目:ave
/**
* Set Cache Adapter
*
* @param \Zend\Cache\Storage\Adapter\AbstractAdapter $cacheAdapter
*/
public function setCacheAdapter($cacheAdapter)
{
$this->cacheAdapter = $cacheAdapter;
$this->cacheAdapter->getOptions()->setTtl($this->getCacheOptions()->getExpires());
}
作者:neophyt
项目:flaming-arche
/**
* Returns sizes array for photo identified by Flickr photo id
*
* @param int $photoId
* @return array Array of photo size information
*/
public function getSizes($photoId)
{
$sizes = $this->adapter->getItem($photoId);
if (is_null($sizes)) {
$sizes = $this->flickr->getSizes($photoId);
$this->adapter->addItem($photoId, $sizes);
}
return $sizes;
}
作者:eltondia
项目:Relogi
/**
* Set options.
*
* @param array|\Traversable|SessionOptions $options
* @return Memory
* @see getOptions()
*/
public function setOptions($options)
{
if (!$options instanceof SessionOptions) {
$options = new SessionOptions($options);
}
return parent::setOptions($options);
}
作者:rawatanil
项目:zf2forbeginners-ol
/**
* All a user to add and update a feed in the application
*
* @return ViewModel
*/
public function manageAction()
{
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('BabyMonitor\\Forms\\ManageRecordForm');
$feedId = (int) $this->params()->fromRoute('id');
if ($this->getRequest()->isGet()) {
if (!empty($feedId)) {
if ($feed = $this->_feedTable->fetchById($feedId)) {
$form->setData($feed->getArrayCopy());
} else {
$this->flashMessenger()->addInfoMessage('Unable to find that feed. Perhaps a new one?');
return $this->redirect()->toRoute(self::DEFAULT_ROUTE, array('action' => 'manage'));
}
}
}
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->isValid()) {
$feed = new FeedModel();
$feed->exchangeArray($form->getData());
$this->_feedTable->save($feed);
if (!is_null($this->_cache)) {
$this->_cache->removeItem(self::KEY_ALL_RESULTS);
}
$this->getEventManager()->trigger('Feed.Modify', $this, array('feedData' => $feed));
return $this->redirect()->toRoute(self::DEFAULT_ROUTE, array());
}
}
return new ViewModel(array('form' => $form, 'cancelTitle' => $feedId ? "Don't update the record" : "Don't create the record", 'messages' => array('info' => $this->flashMessenger()->hasInfoMessages())));
}
作者:CHRISTOPHERVANDOMM
项目:zf2comple
/**
* Set options.
*
* @param array|Traversable|WinCacheOptions $options
* @return WinCache
* @see getOptions()
*/
public function setOptions($options)
{
if (!$options instanceof WinCacheOptions) {
$options = new WinCacheOptions($options);
}
return parent::setOptions($options);
}
作者:nuklehe
项目:zf
/**
* Set options.
*
* @param array|\Traversable|MemoryOptions $options
* @return Memory
* @see getOptions()
*/
public function setOptions($options)
{
if (!$options instanceof MemoryOptions) {
$options = new MemoryOptions($options);
}
return parent::setOptions($options);
}
作者:haoyanfe
项目:zf
/**
* Set options.
*
* @param array|Traversable|ApcOptions $options
* @return Apc
* @see getOptions()
*/
public function setOptions($options)
{
if (!$options instanceof ApcOptions) {
$options = new ApcOptions($options);
}
return parent::setOptions($options);
}
作者:nobesnick
项目:ApiTimesheet
/**
* @param $entity
*
* @return mixed
* @throws \InvalidArgumentException If the ResultSetPrototype is not Hydrating
* @throws \InvalidArgumentException If a BaseEntity object is not provided
*/
public function persist($entity)
{
if (!$this->getResultSetPrototype() instanceof HydratingResultSet) {
throw new \InvalidArgumentException('Result Set Prototype is not configured correctly');
}
if (!$entity instanceof BaseEntity) {
throw new \InvalidArgumentException('Can only persist object entities');
}
$hydrator = $this->getResultSetPrototype()->getHydrator();
$data = $hydrator->extract($entity);
$sql = $this->getSql();
$identifierName = $this->getIdentifierName();
$identifier = null;
if (array_key_exists($identifierName, $data)) {
$identifier = $data[$identifierName];
unset($data[$identifierName]);
}
array_walk($data, function (&$value) {
if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d H:i:s');
}
});
if (!empty($identifier)) {
// UPDATE
$data['lastModified'] = date('Y-m-d H:i:s');
$where = array();
$where[$identifierName] = $identifier;
$statement = $sql->prepareStatementForSqlObject($sql->update()->set($data)->where($where));
$result = $statement->execute();
unset($statement, $result);
// cleanup
} else {
// INSERT
$data['createdDatetime'] = date('Y-m-d H:i:s');
$data['status'] = '1';
$data['lastModified'] = date('Y-m-d H:i:s');
$insert = $sql->insert();
$insert->values($data);
$statement = $sql->prepareStatementForSqlObject($insert);
$result = $statement->execute();
$identifier = $result->getGeneratedValue();
unset($statement, $result);
// cleanup
$where = array();
$where[$identifierName] = $identifier;
}
// refresh data
$statement = $sql->prepareStatementForSqlObject($this->sql->select()->where($where));
$result = $statement->execute();
$rowData = $result->current();
unset($statement, $result);
// cleanup
$hydrator->hydrate($rowData, $entity);
if ($this->cacheAdapter instanceof CacheAdapter) {
$this->cacheAdapter->setItem($this->getCacheKeyHash($entity->getId()), $entity);
}
return $entity;
}
作者:enlitepr
项目:enlite-asseti
/**
* @inheritdoc
*/
public function build()
{
if ($this->built) {
return;
}
$key = 'assets-manager';
if ($this->cacheAdapter->hasItem($key)) {
$data = $this->cacheAdapter->getItem($key);
$assetManager = unserialize($data);
if ($assetManager instanceof AssetManager) {
$this->setAssetManager($assetManager);
return;
}
}
parent::build();
$this->cacheAdapter->setItem($key, serialize($this->getAssetManager()));
$this->built = true;
}
作者:gunnil
项目:WurflCach
/**
* Flush the whole storage
*
* @return bool
*/
public function flush()
{
if (!$this->cache instanceof FlushableInterface) {
return false;
}
try {
return $this->cache->flush();
} catch (ZendException\ExceptionInterface $ex) {
return false;
}
}
作者:BanterMediaS
项目:majestic3-open-sourc
/**
* Remove an item from the cache
* @param string $key
* @return boolean
*/
public function clearItem($key)
{
//check if caching is enabled
$arr_config = $this->getServiceLocator()->get("config");
if ($arr_config["front_end_application_config"]["cache_enabled"] == FALSE) {
return FALSE;
}
//end if
//adjust key
$key = $this->setIdentifier($key);
$this->storageFactory->removeItem($key);
}
作者:navti
项目:xerxes-pazpar
/**
* Constructor
*
* @param array|Traversable|WinCacheOptions $options
* @throws Exception
* @return void
*/
public function __construct($options = null)
{
if (!extension_loaded('wincache')) {
throw new Exception\ExtensionNotLoadedException("WinCache extension is not loaded");
}
$enabled = ini_get('wincache.ucenabled');
if (PHP_SAPI == 'cli') {
$enabled = $enabled && (bool) ini_get('wincache.enablecli');
}
if (!$enabled) {
throw new Exception\ExtensionNotLoadedException("WinCache is disabled - see 'wincache.ucenabled' and 'wincache.enablecli'");
}
parent::__construct($options);
}
作者:parrotcag
项目:ave
/**
* Cache Response for future requests
*
* @param MvcEvent $e
* @return \Zend\Stdlib\ResponseInterface
*/
public function onFinish(MvcEvent $e)
{
$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
return;
}
if (!$request->isGet()) {
return;
}
$response = $e->getResponse();
if ($response instanceof HttpResponse && !$response->isOk()) {
return;
}
// Do not continue if weren't able to compose a key
if (empty($this->cache_key)) {
return;
}
if (!$this->cacheAdapter->hasItem($this->cache_key)) {
$resourceIdentifier = $e->getRouteMatch()->getParam('resource');
$resource = call_user_func($this->getResourceLocatorService(), $resourceIdentifier);
if (!$resource instanceof Resource || !$resource->isCacheable()) {
return;
}
// Generate Response cache headers based on Resource CacheOptions
$cacheOptions = $resource->getCacheOptions();
$cacheControl = new CacheControl();
$cacheControl->addDirective($cacheOptions->getAccess());
$cacheControl->addDirective('max-age', $cacheOptions->getMaxAge());
$cacheControl->addDirective('expires', $cacheOptions->getExpires());
$cacheControl->addDirective('must-revalidate');
$dateTime = new \DateTime();
$dateTime->modify('+ ' . $cacheOptions->getExpires() . 'seconds');
$expires = new Expires();
$expires->setDate($dateTime);
$lastModified = new LastModified();
$lastModified->setDate(new \DateTime());
// Add Headers to Response Header
$response->getHeaders()->addHeader($cacheControl);
$response->getHeaders()->addHeader($expires);
$response->getHeaders()->addHeaderLine('Pragma: ' . $cacheOptions->getAccess());
$response->getHeaders()->addHeader(Etag::fromString('Etag: ' . md5($response->getBody())));
$response->getHeaders()->addHeader($lastModified);
// Set cache adapter's TTL using Resource cache expires value
$this->cacheAdapter->getOptions()->setTtl($cacheOptions->getExpires());
$this->cacheAdapter->setItem($this->cache_key, $response);
//return $response;
}
}
作者:rafalwrzeszc
项目:zf
/**
* Constructor
*
* @param null|array|Traversable|MemcachedOptions $options
* @throws Exception
* @return void
*/
public function __construct($options = null)
{
if (static::$extMemcachedMajorVersion === null) {
$v = (string) phpversion('memcached');
static::$extMemcachedMajorVersion = $v !== '' ? (int) $v[0] : 0;
}
if (static::$extMemcachedMajorVersion < 1) {
throw new Exception\ExtensionNotLoadedException('Need ext/memcached version >= 1.0.0');
}
$this->memcached = new MemcachedResource();
parent::__construct($options);
// It's ok to add server as soon as possible because
// ext/memcached auto-connects to the server on first use
// TODO: Handle multiple servers
$options = $this->getOptions();
$this->memcached->addServer($options->getServer(), $options->getPort());
}
作者:rafalwrzeszc
项目:zf
/**
* Constructor
*
* @param null|array|Traversable|ApcOptions $options
* @throws Exception
* @return void
*/
public function __construct($options = null)
{
if (version_compare('3.1.6', phpversion('apc')) > 0) {
throw new Exception\ExtensionNotLoadedException("Missing ext/apc >= 3.1.6");
}
$enabled = ini_get('apc.enabled');
if (PHP_SAPI == 'cli') {
$enabled = $enabled && (bool) ini_get('apc.enable_cli');
}
if (!$enabled) {
throw new Exception\ExtensionNotLoadedException("ext/apc is disabled - see 'apc.enabled' and 'apc.enable_cli'");
}
// init select map
if (static::$selectMap === null) {
static::$selectMap = array('value' => \APC_ITER_VALUE, 'mtime' => \APC_ITER_MTIME, 'ctime' => \APC_ITER_CTIME, 'atime' => \APC_ITER_ATIME, 'rtime' => \APC_ITER_DTIME, 'ttl' => \APC_ITER_TTL, 'num_hits' => \APC_ITER_NUM_HITS, 'ref_count' => \APC_ITER_REFCOUNT, 'mem_size' => \APC_ITER_MEM_SIZE, 'internal_key' => \APC_ITER_KEY);
}
parent::__construct($options);
}
作者:robertdamo
项目:zf2-module-asset
/**
* @param string $request Request uri
* @throws \RuntimeException
* @return \Zend\Http\PhpEnvironment\Response
*/
public function dispatch($request)
{
if (!$this->getResolver()) {
throw new \RuntimeException("No resolver setted");
}
$asset = $this->resolver->resolve($request);
$content = null;
$responseCode = 404;
$headers = Headers::fromString("Content-Type: text/plain");
if ($asset) {
$headers = $this->getHeaders($asset->getFile(), $asset->getMime());
if ($this->browserCached($asset->getFile())) {
$responseCode = 304;
$headers->addHeader(Connection::fromString("Connection: close"));
} else {
$responseCode = 200;
$cacheKey = "assets-cache-" . md5($request);
if ($this->cache) {
$content = $this->cache->getItem($cacheKey);
}
if (!$content) {
$content = $asset->getContent();
$assetName = end(explode('\\', get_class($asset)));
if (array_key_exists($assetName, $this->filters)) {
foreach ($this->filters[$assetName] as $filter) {
$content = $filter->filter($content);
}
}
if ($this->cache) {
$this->cache->addItem($cacheKey, $content);
}
}
}
} else {
$content = "Asset not found!";
}
$response = new Response();
$response->setStatusCode($responseCode);
$response->setContent($content);
$response->setHeaders($headers);
return $response;
}
作者:sporkcod
项目:spor
/**
* Compile to file. If destination is not specified return CSS.
*
* @param string $source
* @param string|null $destination
* @param array|string|null $include Include path(s) to use
* @throws \Exception on compilation error
* @return string Compiled CSS
*/
public function compile($source, $destination = null, $include = null)
{
$include = array_merge($this->includes, (array) $include);
$useCache = null == $destination && null !== $this->cache;
if ($useCache) {
if ($this->cache->hasItem($source)) {
return $this->cache->getItem($source);
}
}
$arguments = implode(' ', array_map('escapeshellarg', $this->arguments));
$arguments .= ' ' . $this->getCommandArguments($source, $destination, $include);
$command = "{$this->compiler} {$arguments}" . ' 2>&1';
$result = exec($command, $output, $exitCode);
if ($exitCode != 0) {
throw new \Exception(sprintf('Error compiling CSS "%s": %s', $source, implode(PHP_EOL, $output)));
}
$css = implode(PHP_EOL, $output);
if ($useCache) {
$this->cache->setItem($source, $css);
}
return $css;
}
作者:antoineale
项目:HomeStuf
/**
* {@inheritDoc}
* @param Cache $cache
*/
public function __construct($options, Cache $cache)
{
parent::__construct($options);
$this->cache = $cache;
}
作者:till
项目:vufin
/**
* Remove multiple items.
*
* @param array $keys
* @return array Array of not removed keys
* @throws Exception\ExceptionInterface
*
* @triggers removeItems.pre(PreEvent)
* @triggers removeItems.post(PostEvent)
* @triggers removeItems.exception(ExceptionEvent)
*/
public function removeItems(array $keys)
{
$options = $this->getOptions();
if ($options->getWritable() && $options->getClearStatCache()) {
clearstatcache();
}
return parent::removeItems($keys);
}