Cakephp view error - using UploadPack to store images - php

I get the following error when I try to load views from controllers that use the 'UploadPack' - (source:- https://github.com/szajbus/uploadpack):-
Strict (2048): Declaration of UploadBehavior::setup() should be compatible with ModelBehavior::setup(Model $model, $config = Array) [APP\Plugin\upload_pack\Model\Behavior\UploadBehavior.php, line 15]
My 'UploadBehavior.php' and 'ModelBehavior.php' files are below, has anyone got any ideas of how to resolve this error?
<?php
App::uses('HttpSocket', 'Network/Http');
/**
* This file is a part of UploadPack - a plugin that makes file uploads in CakePHP as easy as possible.
*
* UploadBehavior
*
* UploadBehavior does all the job of saving files to disk while saving records to database. For more info read UploadPack documentation.
*
* joe bartlett's lovingly handcrafted tweaks add several resize modes. see "more on styles" in the documentation.
*
* #author Michał Szajbe (michal.szajbe#gmail.com) and joe bartlett (contact#jdbartlett.com)
* #link http://github.com/szajbus/uploadpack
*/
class UploadBehavior extends ModelBehavior {
private static $__settings = array();
private $toWrite = array();
private $toDelete = array();
private $maxWidthSize = false;
public function setup(&$model, $settings = array()) {
$defaults = array(
'path' => ':webroot/upload/:model/:id/:basename_:style.:extension',
'styles' => array(),
'resizeToMaxWidth' => false,
'quality' => 75
);
foreach ($settings as $field => $array) {
self::$__settings[$model->name][$field] = array_merge($defaults, $array);
}
}
public function beforeSave(&$model) {
$this->_reset();
foreach (self::$__settings[$model->name] as $field => $settings) {
if (!empty($model->data[$model->name][$field]) && is_array($model->data[$model->name][$field]) && file_exists($model->data[$model->name][$field]['tmp_name'])) {
if (!empty($model->id)) {
$this->_prepareToDeleteFiles($model, $field, true);
}
$this->_prepareToWriteFiles($model, $field);
unset($model->data[$model->name][$field]);
$model->data[$model->name][$field.'_file_name'] = $this->toWrite[$field]['name'];
$model->data[$model->name][$field.'_file_size'] = $this->toWrite[$field]['size'];
$model->data[$model->name][$field.'_content_type'] = $this->toWrite[$field]['type'];
} elseif (array_key_exists($field, $model->data[$model->name]) && $model->data[$model->name][$field] === null) {
if (!empty($model->id)) {
$this->_prepareToDeleteFiles($model, $field, true);
}
unset($model->data[$model->name][$field]);
$model->data[$model->name][$field.'_file_name'] = null;
$model->data[$model->name][$field.'_file_size'] = null;
$model->data[$model->name][$field.'_content_type'] = null;
}
}
return true;
}
public function afterSave(&$model, $create) {
if (!$create) {
$this->_deleteFiles($model);
}
$this->_writeFiles($model);
}
public function beforeDelete(&$model) {
$this->_reset();
$this->_prepareToDeleteFiles($model);
return true;
}
public function afterDelete(&$model) {
$this->_deleteFiles($model);
}
public function beforeValidate(&$model) {
foreach (self::$__settings[$model->name] as $field => $settings) {
if (isset($model->data[$model->name][$field])) {
$data = $model->data[$model->name][$field];
if ((empty($data) || is_array($data) && empty($data['tmp_name'])) && !empty($settings['urlField']) && !empty($model->data[$model->name][$settings['urlField']])) {
$data = $model->data[$model->name][$settings['urlField']];
}
if (!is_array($data)) {
$model->data[$model->name][$field] = $this->_fetchFromUrl($data);
}
}
}
return true;
}
private function _reset() {
$this->toWrite = null;
$this->toDelete = null;
}
private function _fetchFromUrl($url) {
$data = array('remote' => true);
$data['name'] = end(explode('/', $url));
$data['tmp_name'] = tempnam(sys_get_temp_dir(), $data['name']) . '.' . end(explode('.', $url));
$httpSocket = new HttpSocket();
$raw = $httpSocket->get($url);
$response = $httpSocket->response;
$data['size'] = strlen($raw);
$data['type'] = reset(explode(';', $response['header']['Content-Type']));
file_put_contents($data['tmp_name'], $raw);
return $data;
}
private function _prepareToWriteFiles(&$model, $field) {
$this->toWrite[$field] = $model->data[$model->name][$field];
// make filename URL friendly by using Cake's Inflector
$this->toWrite[$field]['name'] =
Inflector::slug(substr($this->toWrite[$field]['name'], 0, strrpos($this->toWrite[$field]['name'], '.'))). // filename
substr($this->toWrite[$field]['name'], strrpos($this->toWrite[$field]['name'], '.')); // extension
}
private function _writeFiles(&$model) {
if (!empty($this->toWrite)) {
foreach ($this->toWrite as $field => $toWrite) {
$settings = $this->_interpolate($model, $field, $toWrite['name'], 'original');
$destDir = dirname($settings['path']);
if (!file_exists($destDir)) {
#mkdir($destDir, 0777, true);
#chmod($destDir, 0777);
}
if (is_dir($destDir) && is_writable($destDir)) {
$move = !empty($toWrite['remote']) ? 'rename' : 'move_uploaded_file';
if (#$move($toWrite['tmp_name'], $settings['path'])) {
if($this->maxWidthSize) {
$this->_resize($settings['path'], $settings['path'], $this->maxWidthSize.'w', $settings['quality']);
}
foreach ($settings['styles'] as $style => $geometry) {
$newSettings = $this->_interpolate($model, $field, $toWrite['name'], $style);
$this->_resize($settings['path'], $newSettings['path'], $geometry, $settings['quality']);
}
}
}
}
}
}
private function _prepareToDeleteFiles(&$model, $field = null, $forceRead = false) {
$needToRead = true;
if ($field === null) {
$fields = array_keys(self::$__settings[$model->name]);
foreach ($fields as &$field) {
$field .= '_file_name';
}
} else {
$field .= '_file_name';
$fields = array($field);
}
if (!$forceRead && !empty($model->data[$model->alias])) {
$needToRead = false;
foreach ($fields as $field) {
if (!array_key_exists($field, $model->data[$model->alias])) {
$needToRead = true;
break;
}
}
}
if ($needToRead) {
$data = $model->find('first', array('conditions' => array($model->alias.'.'.$model->primaryKey => $model->id), 'fields' => $fields, 'callbacks' => false));
} else {
$data = $model->data;
}
if (is_array($this->toDelete)) {
$this->toDelete = array_merge($this->toDelete, $data[$model->alias]);
} else {
$this->toDelete = $data[$model->alias];
}
$this->toDelete['id'] = $model->id;
}
private function _deleteFiles(&$model) {
foreach (self::$__settings[$model->name] as $field => $settings) {
if (!empty($this->toDelete[$field.'_file_name'])) {
$styles = array_keys($settings['styles']);
$styles[] = 'original';
foreach ($styles as $style) {
$settings = $this->_interpolate($model, $field, $this->toDelete[$field.'_file_name'], $style);
if (file_exists($settings['path'])) {
#unlink($settings['path']);
}
}
}
}
}
private function _interpolate(&$model, $field, $filename, $style) {
return self::interpolate($model->name, $model->id, $field, $filename, $style);
}
static public function interpolate($modelName, $modelId, $field, $filename, $style = 'original', $defaults = array()) {
$pathinfo = UploadBehavior::_pathinfo($filename);
$interpolations = array_merge(array(
'app' => preg_replace('/\/$/', '', APP),
'webroot' => preg_replace('/\/$/', '', WWW_ROOT),
'model' => Inflector::tableize($modelName),
'basename' => !empty($filename) ? $pathinfo['filename'] : null,
'extension' => !empty($filename) ? $pathinfo['extension'] : null,
'id' => $modelId,
'style' => $style,
'attachment' => Inflector::pluralize($field),
'hash' => md5((!empty($filename) ? $pathinfo['filename'] : "") . Configure::read('Security.salt'))
), $defaults);
$settings = self::$__settings[$modelName][$field];
$keys = array('path', 'url', 'default_url');
foreach ($interpolations as $k => $v) {
foreach ($keys as $key) {
if (isset($settings[$key])) {
$settings[$key] = preg_replace('/\/{2,}/', '/', str_replace(":$k", $v, $settings[$key]));
}
}
}
return $settings;
}
static private function _pathinfo($filename) {
$pathinfo = pathinfo($filename);
// PHP < 5.2.0 doesn't include 'filename' key in pathinfo. Let's try to fix this.
if (empty($pathinfo['filename'])) {
$suffix = !empty($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
$pathinfo['filename'] = basename($pathinfo['basename'], $suffix);
}
return $pathinfo;
}
private function _resize($srcFile, $destFile, $geometry, $quality = 75) {
copy($srcFile, $destFile);
#chmod($destFile, 0777);
$pathinfo = UploadBehavior::_pathinfo($srcFile);
$src = null;
$createHandler = null;
$outputHandler = null;
switch (strtolower($pathinfo['extension'])) {
case 'gif':
$createHandler = 'imagecreatefromgif';
$outputHandler = 'imagegif';
break;
case 'jpg':
case 'jpeg':
$createHandler = 'imagecreatefromjpeg';
$outputHandler = 'imagejpeg';
break;
case 'png':
$createHandler = 'imagecreatefrompng';
$outputHandler = 'imagepng';
$quality = null;
break;
default:
return false;
}
if ($src = $createHandler($destFile)) {
$srcW = imagesx($src);
$srcH = imagesy($src);
// determine destination dimensions and resize mode from provided geometry
if (preg_match('/^\\[[\\d]+x[\\d]+\\]$/', $geometry)) {
// resize with banding
list($destW, $destH) = explode('x', substr($geometry, 1, strlen($geometry)-2));
$resizeMode = 'band';
} elseif (preg_match('/^[\\d]+x[\\d]+$/', $geometry)) {
// cropped resize (best fit)
list($destW, $destH) = explode('x', $geometry);
$resizeMode = 'best';
} elseif (preg_match('/^[\\d]+w$/', $geometry)) {
// calculate heigh according to aspect ratio
$destW = (int)$geometry-1;
$resizeMode = false;
} elseif (preg_match('/^[\\d]+h$/', $geometry)) {
// calculate width according to aspect ratio
$destH = (int)$geometry-1;
$resizeMode = false;
} elseif (preg_match('/^[\\d]+l$/', $geometry)) {
// calculate shortest side according to aspect ratio
if ($srcW > $srcH) $destW = (int)$geometry-1;
else $destH = (int)$geometry-1;
$resizeMode = false;
}
if (!isset($destW)) $destW = ($destH/$srcH) * $srcW;
if (!isset($destH)) $destH = ($destW/$srcW) * $srcH;
// determine resize dimensions from appropriate resize mode and ratio
if ($resizeMode == 'best') {
// "best fit" mode
if ($srcW > $srcH) {
if ($srcH/$destH > $srcW/$destW) $ratio = $destW/$srcW;
else $ratio = $destH/$srcH;
} else {
if ($srcH/$destH < $srcW/$destW) $ratio = $destH/$srcH;
else $ratio = $destW/$srcW;
}
$resizeW = $srcW*$ratio;
$resizeH = $srcH*$ratio;
}
elseif ($resizeMode == 'band') {
// "banding" mode
if ($srcW > $srcH) $ratio = $destW/$srcW;
else $ratio = $destH/$srcH;
$resizeW = $srcW*$ratio;
$resizeH = $srcH*$ratio;
}
else {
// no resize ratio
$resizeW = $destW;
$resizeH = $destH;
}
$img = imagecreatetruecolor($destW, $destH);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
imagecopyresampled($img, $src, ($destW-$resizeW)/2, ($destH-$resizeH)/2, 0, 0, $resizeW, $resizeH, $srcW, $srcH);
$outputHandler($img, $destFile, $quality);
return true;
}
return false;
}
public function attachmentMinSize(&$model, $value, $min) {
$value = array_shift($value);
if (!empty($value['tmp_name'])) {
return (int)$min <= (int)$value['size'];
}
return true;
}
public function attachmentMaxSize(&$model, $value, $max) {
$value = array_shift($value);
if (!empty($value['tmp_name'])) {
return (int)$value['size'] <= (int)$max;
}
return true;
}
public function attachmentContentType(&$model, $value, $contentTypes) {
$value = array_shift($value);
if (!is_array($contentTypes)) {
$contentTypes = array($contentTypes);
}
if (!empty($value['tmp_name'])) {
foreach ($contentTypes as $contentType) {
if (substr($contentType, 0, 1) == '/') {
if (preg_match($contentType, $value['type'])) {
return true;
}
} elseif ($contentType == $value['type']) {
return true;
}
}
return false;
}
return true;
}
public function attachmentPresence(&$model, $value) {
$keys = array_keys($value);
$field = $keys[0];
$value = array_shift($value);
if (!empty($value['tmp_name'])) {
return true;
}
if (!empty($model->id)) {
if (!empty($model->data[$model->alias][$field.'_file_name'])) {
return true;
} elseif (!isset($model->data[$model->alias][$field.'_file_name'])) {
$existingFile = $model->field($field.'_file_name', array($model->primaryKey => $model->id));
if (!empty($existingFile)) {
return true;
}
}
}
return false;
}
public function minWidth(&$model, $value, $minWidth) {
return $this->_validateDimension($value, 'min', 'x', $minWidth);
}
public function minHeight(&$model, $value, $minHeight) {
return $this->_validateDimension($value, 'min', 'y', $minHeight);
}
public function maxWidth(&$model, $value, $maxWidth) {
$keys = array_keys($value);
$field = $keys[0];
$settings = self::$__settings[$model->name][$field];
if($settings['resizeToMaxWidth'] && !$this->_validateDimension($value, 'max', 'x', $maxWidth)) {
$this->maxWidthSize = $maxWidth;
return true;
} else {
return $this->_validateDimension($value, 'max', 'x', $maxWidth);
}
}
public function maxHeight(&$model, $value, $maxHeight) {
return $this->_validateDimension($value, 'max', 'y', $maxHeight);
}
private function _validateDimension($upload, $mode, $axis, $value) {
$upload = array_shift($upload);
$func = 'images'.$axis;
if(!empty($upload['tmp_name'])) {
$createHandler = null;
if($upload['type'] == 'image/jpeg') {
$createHandler = 'imagecreatefromjpeg';
} else if($upload['type'] == 'image/gif') {
$createHandler = 'imagecreatefromgif';
} else if($upload['type'] == 'image/png') {
$createHandler = 'imagecreatefrompng';
} else {
return false;
}
if($img = $createHandler($upload['tmp_name'])) {
switch ($mode) {
case 'min':
return $func($img) >= $value;
break;
case 'max':
return $func($img) <= $value;
break;
}
}
}
return false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//My 'ModelBehavior' file:-
<?php
/**
* Model behaviors base class.
*
* Adds methods and automagic functionality to Cake Models.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* #copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* #link http://cakephp.org CakePHP(tm) Project
* #package Cake.Model
* #since CakePHP(tm) v 1.2.0.0
* #license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Model behavior base class.
*
* Defines the Behavior interface, and contains common model interaction functionality. Behaviors
* allow you to simulate mixins, and create reusable blocks of application logic, that can be reused across
* several models. Behaviors also provide a way to hook into model callbacks and augment their behavior.
*
* ### Mixin methods
*
* Behaviors can provide mixin like features by declaring public methods. These methods should expect
* the model instance to be shifted onto the parameter list.
*
* {{{
* function doSomething(Model $model, $arg1, $arg2) {
* //do something
* }
* }}}
*
* Would be called like `$this->Model->doSomething($arg1, $arg2);`.
*
* ### Mapped methods
*
* Behaviors can also define mapped methods. Mapped methods use pattern matching for method invocation. This
* allows you to create methods similar to Model::findAllByXXX methods on your behaviors. Mapped methods need to
* be declared in your behaviors `$mapMethods` array. The method signature for a mapped method is slightly different
* than a normal behavior mixin method.
*
* {{{
* public $mapMethods = array('/do(\w+)/' => 'doSomething');
*
* function doSomething(Model $model, $method, $arg1, $arg2) {
* //do something
* }
* }}}
*
* The above will map every doXXX() method call to the behavior. As you can see, the model is
* still the first parameter, but the called method name will be the 2nd parameter. This allows
* you to munge the method name for additional information, much like Model::findAllByXX.
*
* #package Cake.Model
* #see Model::$actsAs
* #see BehaviorCollection::load()
*/
class ModelBehavior extends Object {
/**
* Contains configuration settings for use with individual model objects. This
* is used because if multiple models use this Behavior, each will use the same
* object instance. Individual model settings should be stored as an
* associative array, keyed off of the model name.
*
* #var array
* #see Model::$alias
*/
public $settings = array();
/**
* Allows the mapping of preg-compatible regular expressions to public or
* private methods in this class, where the array key is a /-delimited regular
* expression, and the value is a class method. Similar to the functionality of
* the findBy* / findAllBy* magic methods.
*
* #var array
*/
public $mapMethods = array();
/**
* Setup this behavior with the specified configuration settings.
*
* #param Model $model Model using this behavior
* #param array $config Configuration settings for $model
* #return void
*/
public function setup(Model $model, $config = array()) {
}
/**
* Clean up any initialization this behavior has done on a model. Called when a behavior is dynamically
* detached from a model using Model::detach().
*
* #param Model $model Model using this behavior
* #return void
* #see BehaviorCollection::detach()
*/
public function cleanup(Model $model) {
if (isset($this->settings[$model->alias])) {
unset($this->settings[$model->alias]);
}
}
/**
* beforeFind can be used to cancel find operations, or modify the query that will be executed.
* By returning null/false you can abort a find. By returning an array you can modify/replace the query
* that is going to be run.
*
* #param Model $model Model using this behavior
* #param array $query Data used to execute this query, i.e. conditions, order, etc.
* #return boolean|array False or null will abort the operation. You can return an array to replace the
* $query that will be eventually run.
*/
public function beforeFind(Model $model, $query) {
return true;
}
/**
* After find callback. Can be used to modify any results returned by find.
*
* #param Model $model Model using this behavior
* #param mixed $results The results of the find operation
* #param boolean $primary Whether this model is being queried directly (vs. being queried as an association)
* #return mixed An array value will replace the value of $results - any other value will be ignored.
*/
public function afterFind(Model $model, $results, $primary) {
}
/**
* beforeValidate is called before a model is validated, you can use this callback to
* add behavior validation rules into a models validate array. Returning false
* will allow you to make the validation fail.
*
* #param Model $model Model using this behavior
* #return mixed False or null will abort the operation. Any other result will continue.
*/
public function beforeValidate(Model $model) {
return true;
}
/**
* afterValidate is called just after model data was validated, you can use this callback
* to perform any data cleanup or preparation if needed
*
* #param Model $model Model using this behavior
* #return mixed False will stop this event from being passed to other behaviors
*/
public function afterValidate(Model $model) {
return true;
}
/**
* beforeSave is called before a model is saved. Returning false from a beforeSave callback
* will abort the save operation.
*
* #param Model $model Model using this behavior
* #return mixed False if the operation should abort. Any other result will continue.
*/
public function beforeSave(Model $model) {
return true;
}
/**
* afterSave is called after a model is saved.
*
* #param Model $model Model using this behavior
* #param boolean $created True if this save created a new record
* #return boolean
*/
public function afterSave(Model $model, $created) {
return true;
}
/**
* Before delete is called before any delete occurs on the attached model, but after the model's
* beforeDelete is called. Returning false from a beforeDelete will abort the delete.
*
* #param Model $model Model using this behavior
* #param boolean $cascade If true records that depend on this record will also be deleted
* #return mixed False if the operation should abort. Any other result will continue.
*/
public function beforeDelete(Model $model, $cascade = true) {
return true;
}
/**
* After delete is called after any delete occurs on the attached model.
*
* #param Model $model Model using this behavior
* #return void
*/
public function afterDelete(Model $model) {
}
/**
* DataSource error callback
*
* #param Model $model Model using this behavior
* #param string $error Error generated in DataSource
* #return void
*/
public function onError(Model $model, $error) {
}
/**
* If $model's whitelist property is non-empty, $field will be added to it.
* Note: this method should *only* be used in beforeValidate or beforeSave to ensure
* that it only modifies the whitelist for the current save operation. Also make sure
* you explicitly set the value of the field which you are allowing.
*
* #param Model $model Model using this behavior
* #param string $field Field to be added to $model's whitelist
* #return void
*/
protected function _addToWhitelist(Model $model, $field) {
if (is_array($field)) {
foreach ($field as $f) {
$this->_addToWhitelist($model, $f);
}
return;
}
if (!empty($model->whitelist) && !in_array($field, $model->whitelist)) {
$model->whitelist[] = $field;
}
}
}

The signature of the setup() method of the UploadBehavior should be the same as the signature of the setup() method in the parent class. This means you have to change: public function setup(&$model, $settings = array()) in your UploadBehavior to public function setup(Model $model, $settings = array()) and the error should go away.

Related

How to disable _PHCR key pefixes used in Phalcon Redis backend

I'm using Phalcon Redis backend to store some data. I later try to access this data in Lua language embedded into nginx. What drives me crazy is that Phalcon adds some garbage prefixes to Redis keys and some terrible prefixes to values. So, if I store this pair in Redis - (abc, querty) - this is what is really stored:
(_PHCRabc, s:6:"querty")
Is it possible to disable all this garbage and continue working with Phalcon Redis backend?
According to the the source it is not possible to disable it with option: https://github.com/phalcon/cphalcon/blob/master/phalcon/cache/backend/redis.zep
public function get(string keyName, int lifetime = null) -> var | null
let lastKey = "_PHCR" . prefix . keyName;
public function save(keyName = null, content = null, lifetime = null, boolean stopBuffer = true) -> boolean
lastKey = "_PHCR" . prefixedKey,
Also quoting the docs:
This adapter uses the special redis key “_PHCR” to store all the keys
internally used by the adapter
I read somewhere that this is done in order to be able to flush Phalcon generated cache files.
Your best option would be to extend the \Phalcon\Cache\Backend\Redis class and overwrite the save/get methods. And after use your class in the service:
// Cache
$di->setShared('cache', function() use ($config) {
return new MyCustomRedis(
new \Phalcon\Cache\Frontend\Json(['lifetime' => 172800]), // 2d
$config->redis
);
});
You can override the redis adapter like this.
<?php
namespace App\Library\Cache\Backend;
use Phalcon\Cache\Exception;
class Redis extends \Phalcon\Cache\Backend\Redis
{
/**
* #var \Redis
*/
protected $_redis;
/**
* {#inheritdoc}
*
* #param string $keyName
* #param integer $lifetime
* #return mixed|null
*/
public function get($keyName, $lifetime = null)
{
$redis = $this->getRedis();
/**
* #var \Phalcon\Cache\FrontendInterface $frontend
*/
$frontend = $this->_frontend;
$lastKey = $this->getKeyName($keyName);
$this->_lastKey = $lastKey;
$content = $redis->get($lastKey);
if ($content === false) {
return null;
}
if (is_numeric($content)) {
return $content;
}
return $frontend->afterRetrieve($content);
}
/**
* {#inheritdoc}
*
* #param string $keyName
* #param string $content
* #param int $lifetime
* #param bool $stopBuffer
* #return bool
*
* #throws Exception
*/
public function save($keyName = null, $content = null, $lifetime = null, $stopBuffer = true)
{
if ($keyName === null) {
$lastKey = $this->_lastKey;
} else {
$lastKey = $this->getKeyName($keyName);
$this->_lastKey = $lastKey;
}
if (!$lastKey) {
throw new Exception('The cache must be started first');
}
$redis = $this->getRedis();
/**
* #var \Phalcon\Cache\FrontendInterface $frontend
*/
$frontend = $this->_frontend;
if ($content === null) {
$cachedContent = $frontend->getContent();
} else {
$cachedContent = $content;
}
/**
* Prepare the content in the frontend
*/
if (!is_numeric($cachedContent)) {
$preparedContent = $frontend->beforeStore($cachedContent);
} else {
$preparedContent = $cachedContent;
}
if ($lifetime === null) {
$tmp = $this->_lastLifetime;
$ttl = $tmp ? $tmp : $frontend->getLifetime();
} else {
$ttl = $lifetime;
}
$success = $redis->set($lastKey, $preparedContent);
if (!$success) {
throw new Exception('Failed storing the data in redis');
}
if ($ttl > 0) {
$redis->setTimeout($lastKey, $ttl);
}
$isBuffering = $frontend->isBuffering();
if ($stopBuffer === true) {
$frontend->stop();
}
if ($isBuffering === true) {
echo $cachedContent;
}
$this->_started = false;
return $success;
}
/**
* {#inheritdoc}
*
* #param string $keyName
* #return bool
*/
public function delete($keyName)
{
$redis = $this->getRedis();
$lastKey = $this->getKeyName($keyName);
return (bool)$redis->delete($lastKey);
}
/**
* {#inheritdoc}
*
* #param string $prefix
* #return array
*/
public function queryKeys($prefix = null)
{
$redis = $this->getRedis();
$pattern = "{$this->_prefix}" . ($prefix ? $prefix : '') . '*';
return $redis->keys($pattern);
}
/**
* {#inheritdoc}
*
* #param string $keyName
* #param string $lifetime
* #return bool
*/
public function exists($keyName = null, $lifetime = null)
{
$redis = $this->getRedis();
if ($keyName === null) {
$lastKey = $this->_lastKey;
} else {
$lastKey = $this->getKeyName($keyName);
}
return (bool)$redis->exists($lastKey);
}
/**
* {#inheritdoc}
*
* #param string $keyName
* #param int $value
* #return int
*/
public function increment($keyName = null, $value = 1)
{
$redis = $this->getRedis();
if ($keyName === null) {
$lastKey = $this->_lastKey;
} else {
$lastKey = $this->getKeyName($keyName);
}
return $redis->incrBy($lastKey, $value);
}
/**
* {#inheritdoc}
*
* #param string $keyName
* #param int $value
* #return int
*/
public function decrement($keyName = null, $value = 1)
{
$redis = $this->getRedis();
if ($keyName === null) {
$lastKey = $this->_lastKey;
} else {
$lastKey = $this->getKeyName($keyName);
}
return $redis->decrBy($lastKey, $value);
}
/**
* {#inheritdoc}
*
* #return bool
*/
public function flush()
{
}
/**
* Get Prefix
*
* #return string
*/
public function getPrefix()
{
return $this->_prefix;
}
/**
* Get Redis Connection
*
* #return \Redis
*/
public function getRedis()
{
$redis = $this->_redis;
if (!is_object($redis)) {
$this->_connect();
$redis = $this->_redis;
}
return $redis;
}
/**
* Get Key Name
*
* #param $keyName
* #return string
*/
protected function getKeyName($keyName)
{
return $this->_prefix . $keyName;
}
}

Kohana - display buffered views or custom view on shutdown

I am using Kohana View_Core derivative for my views. I would like to display buffered views or custom view on shutdown e.g.
If there were some errors/exceptions during the runtime => display error page
If executed with no errors => display the buffered output
The View
class View
{
private $file = '';
private $data = array();
private static $global_data = array();
public static $view = '';
private function __construct($file = FALSE, array $data = array())
{
if ($file !== FALSE)
{
$this->set_filename($file);
}
if (!empty($data))
{
$this->data = $data + $this->data;
}
}
/**
* Creates new view object and returns it.
*
* #param string filename
* #param array variables
* #return object View
*/
public static function factory($file = FALSE, array $data = array())
{
return new View($file, $data);
}
/**
* Captures the output that is generated when a view is included.
* The view data will be extracted to make local variables. This method
* is static to prevent object scope resolution.
*
* #param string filename
* #param array variables
* #return string
*/
public static function capture($view_filename, array $view_data)
{
extract($view_data, EXTR_SKIP);
ob_start();
try
{
require $view_filename;
}
catch (Exception $error)
{
$ob_handlers = ob_list_handlers();
if (!empty($ob_handlers))
{
ob_end_clean();
}
throw $error;
}
return ob_get_clean();
}
/**
* Load view file
*
* #param string filename
* #return boolean
* #return object View
*/
public function set_filename($file)
{
if (strpos($file, APP_DIR) === FALSE)
{
$extension = strrpos($file, '.') === FALSE ? '.php' : '';
$path = APP_DIR.DIR_SEP.'system'.DIR_SEP.'views'.DIR_SEP.$file.$extension;
}
else
{
$path = $file;
}
if (!file_exists($path))
{
Error::throw_throwable('Unable to find file '.$path);
}
$this->file = $path;
return $this;
}
/**
* Sets a global variable, similar to the set() method.
*
* #param string variable name
* #param mixed variable value
* #return object View
*/
public static function set_global($key, $value = FALSE)
{
self::$global_data[$key] = $value;
}
/**
* Assigns a variable by name.
*
* #param string variable name or an array of variables
* #param mixed variable value
* #return object View
*/
public function set($key, $value = FALSE)
{
if (is_array($key))
{
foreach ($key as $name => $value)
{
$this->data[$name] = $value;
}
}
else
{
$this->data[$key] = $value;
}
return $this;
}
/**
* Renders the view object to a string.
*
* #throws exception
* #param string filename
* #return string
*/
public function render($file = FALSE)
{
if ($file !== FALSE)
{
$this->set_filename($file);
}
if (empty($this->file))
{
Error::throw_throwable('Unable to find file '.$this->file);
}
$data = array_merge(View::$global_data, $this->data);
return View::capture($this->file, $data);
}
public function __toString()
{
try
{
$result = $this->render();
return $result;
}
catch (Exception $error)
{
Error::throw_throwable($error);
}
}
public function __set($key, $value)
{
$this->set($key, $value);
}
public function __get($key)
{
return isset($this->data[$key]) ? $this->data[$key] : FALSE;
}
}
Usage
$content = View::factory('main/Main')
->set('order', !empty($_SESSION['order']) ? $_SESSION['order'] : FALSE)
->set('order_success', $order_success)
->set('items', $items)
->set('weights', $weights)
->set('ammounts', $ammounts)
->set('prices', $prices)
->set('total_price', $total_price)
->set('validator', FALSE)
;
$template = $this->get_template();
echo $template->set('content', $content);
What the previous lines do is echo a content view inside of template view. And this should be also the result that my shutdown handler is echoing if necessary. Is there a nice/easy way to do this?
It sounds like what you want is to show a custom error page when there is an error. What I typically do here is to extend Kohana_Kohana_Exception class (as Kohana_Exception) and override the public static function handler method.
This lets you put in some code that can check what the error is (HTTP_404_Exception or other) what the environment is (dev / production) and do whatever behavior you want.
In my case, something like this:
// If not production and not an HTTP exception
if ( ! ($e instanceof HTTP_Exception) && Kohana::$environment !== Kohana::PRODUCTION)
{
// Use built in error handler to show stace trace for developers
Kohana_Kohana_Exception::handler($e);
// ... exit(1);
}
// Otherwise
$injected_routes = array(Route::get('error'));
echo Request::factory('error-uri', NULL, FALSE, $injected_routes)
->headers(Request::$initial->headers())
->execute()
->send_headers(TRUE)
->body();
Here you need another route called error that matches error-uri and goes to another error controller/action/view that you can use to render your custom error page.
For this to work you need to have errors enabled by passing 'errors' => TRUE to your Kohana::init call in bootstrap.php.

Create an A* search with PHP

i have a map stored as a multidimensional array ($map[row][col]) and i'd wish to create a path from point A to point B.
since i can have some obstacles with turns, corners etc etc, i'd wish to use the A* search to calculate the fastest path.
so the general function is
f(x) = g(x) + h(x)
and i have all of these values. g(x) is cost of the move (and it's saved on the map); h(x) is the linear distance between A and B.
so i have everything i need, but i have a question: how can i organize everything?
i have no need to test for alternative paths, since a square on the map can be passable or not, so when i reach the target it should be the shortest one.
how can i organize everything?
i tried with multidimensional array, but i get lost.. :(
EDIT
i worked out some code, it's pretty a wall of text :)
//$start = array(28, 19), $end = array(14, 19)
//$this->map->map is a multidimensional array, everything has a cost of 1, except for
//blocking squares that cost 99
//$this->map->map == $this->radar
//blocking square at 23-17, 22-18, 22-19, 22-20, 23-21, 19-17, 20-18,20-19,20-20,19-21
//they are like 2 specular mustache :P
function createPath($start, $end)
{
$found = false;
$temp = $this->cost($start, $end);
foreach($temp as $t){
if($t['cost'] == $this->map->map[$end[0]][$end[1]]) $found = true;
$this->costStack[$t['cost']][] = array('grid' => $t['grid'], 'dir' => $t['dir']);
}
ksort($this->costStack);
if(!$found) {
foreach($this->costStack as $k => $stack){
foreach($stack as $kn => $node){
$curNode = $node['grid'];
unset($this->costStack[$k][$kn]);
break;
}
if(!count($this->costStack[$k])) unset($this->costStack[$k]);
break;
}
$this->createPath($curNode, $end);
}
}
function cost($current, $target)
{
$return = array();
//$AIM = array('n' => array(-1, 0),'e' => array( 0, 1),'s' => array( 1, 0),'w' => array( 0, -1));
foreach($this->AIM as $direction => $offset){
$position[0] = $current[0] + $offset[0];
$position[1] = $current[1] + $offset[1];
//radar is a copy of the map
if ( $this->radar[$position[0]][$position[1]] == 'V') continue;
else $this->radar[$position[0]][$position[1]] = 'V';
$h = (int) $this->distance($position, $target);
$g = $this->map->map[$position[0]][$position[1]];
$return[] = array('grid' => $position,
'dir' => $direction,
'cost' => $h + $g);
}
return $return;
}
i hope you can understand everything, i tried to be clear as much as possible.
finally i can get to my destination, expanding only cheaper nodes, but now i have a problem.
how can i turn it into directions? i have to store a stack of orders (ie n, n, e etc etc), how can i identify a path inside these values?
My structure was:
Have a Grid-class for holding all possible nodes (propably your array
goes here)
Have a Node-class representing the nodes. Nodes will also calculated costs and store predecessor/g-values set by AStar
Have a AStar class, which will only get two nodes (e.g. startNode, endNode)
Have a PriorityQueue as your open-list
when a Node is asked (by AStar) about it's neighbors, delegated that call to Grid
I'll try to collect some code samples from a prior project, could take a while though.
Update
(found my old project ;))
It's probably not exactly what you're looking for, but maybe it's a start.
So using the files below, and mazes defined like:
00000000000000000000000
00000000000000000000000
0000000000W000000000000
0000000000W000000000000
0000000000W000000000000
0000000000W00000WWWWWWW
0000000000W000000000000
S000000000W00000000000E
(test/maze.txt)
You'll get something like this:
00000000000000000000000
0000000000X000000000000
000000000XWXX0000000000
00000000X0W00X000000000
000000XX00W000X00000000
00000X0000W0000XWWWWWWW
0000X00000W00000XXX0000
SXXX000000W00000000XXXE
index.php
error_reporting(E_ALL ^ E_STRICT);
ini_set('display_errors', 'on');
header('Content-Type: text/plain; charset="utf-8"');
// simple autoloader
function __autoload($className) {
$path = '/lib/' . str_replace('_', '/', $className) . '.php';
foreach (explode(PATH_SEPARATOR, get_include_path()) as $prefix) {
if (file_exists($prefix . $path)) {
require_once $prefix . $path;
}
}
}
// init maze
$maze = new Maze_Reader('test/maze.txt');
$startNode = $maze->getByValue('S', true);
$endNode = $maze->getByValue('E', true);
$astar = new AStar;
if ($astar->getPath($startNode, $endNode)) {
do {
if (!in_array($endNode->value(), array('S', 'E'))) {
$endNode->value('X');
}
} while ($endNode = $endNode->predecessor());
}
echo $maze;
/lib/AStar.php
/**
* A simple AStar implementation
*/
class AStar
{
protected $openList;
protected $closedList;
/**
* Constructs the astar object
*/
public function __construct() {
$this->openList = new PriorityQueue;
$this->closedList = new SplObjectStorage;
}
public function getPath($startNode, $endNode) {
$this->openList->insert(0, $startNode);
while (!$this->openList->isEmpty()) {
$currentNode = $this->openList->extract();
if ($currentNode->equals($endNode)) {
return $currentNode;
}
$this->expandNode($currentNode, $endNode);
$this->closedList[$currentNode] = true;
}
return false;
}
protected function expandNode($currentNode, $endNode) {
foreach ($currentNode->successors() as $successor) {
if (isset($this->closedList[$successor])) {
continue;
}
$tentative_g = $currentNode->g() + $currentNode->distance($successor);
if ($this->openList->indexOf($successor) > -1 && $tentative_g >= $successor->g()) {
continue;
}
$successor->predecessor($currentNode);
$successor->g($tentative_g);
$f = $tentative_g + $successor->distance($endNode);
if ($this->openList->indexOf($successor) > -1) {
$this->openList->changeKey($successor, $f);
continue;
}
$this->openList->insert($f, $successor);
}
}
}
/lib/PriorityQueue.php
class PriorityQueue
{
protected $keys = array();
protected $values = array();
/**
* Helper function to swap two <key>/<value> pairs
*
* #param Integer a
* #param Integer b
* #return Integer b
*/
protected function swap($a, $b) {
// swap keys
$c = $this->keys[$a];
$this->keys[$a] = $this->keys[$b];
$this->keys[$b] = $c;
// swap values
$c = $this->values[$a];
$this->values[$a] = $this->values[$b];
$this->values[$b] = $c;
return $b;
}
/**
* Heapify up
*
* #param Integer pos
* #return void
*/
protected function upHeap($pos) {
while ($pos > 0) {
$parent = ($pos - 1) >> 2;
if ($this->compare($this->keys[$pos], $this->keys[$parent]) >= 0) {
break;
}
$pos = $this->swap($pos, $parent);
}
}
/**
* Heapify down
*
* #param Integer pos
* #return void
*/
protected function downHeap($pos) {
$len = sizeof($this->keys);
$max = ($len - 1) / 2;
while ($pos < $max) {
$child = 2 * $pos + 1;
if ($child < $len - 1 && $this->compare($this->keys[$child], $this->keys[$child + 1]) > 0) {
$child += 1;
}
if ($this->compare($this->keys[$pos], $this->keys[$child]) <= 0) {
break;
}
$pos = $this->swap($pos, $child);
}
}
/**
* Insert an <key>/<value> pair into the queue
*
* #param Object key
* #param Object value
* #return this
*/
public function insert($key, $value) {
$this->keys[] = $key;
$this->values[] = $value;
$this->upHeap(sizeof($this->keys) - 1);
return $this;
}
/**
* Extract the top <value>
*
* #return Object
*/
public function extract() {
$resultValue = $this->values[0];
$lastValue = array_pop($this->values);
$lastKey = array_pop($this->keys);
if (sizeof($this->keys) > 0) {
$this->values[0] = $lastValue;
$this->keys[0] = $lastKey;
$this->downHeap(0);
}
return $resultValue;
}
/**
* Changes the <key> of a <value>
*
* #param Object key
* #param Object value
* #return this
*/
public function changeKey($key, $value) {
$pos = $this->indexOf($value);
if ($pos !== false) {
$this->keys[$pos] = $key;
$this->upHeap($pos);
}
return $this;
}
/**
* Returns the index of <value> or false if <value> is not in the queue
*
* #return false|Int
*/
public function indexOf($value) {
return array_search($value, $this->values, true);
}
/**
* Used to campare two <key>s.
*
* #param Object a
* #param Object b
* #return Number
*/
protected function compare($a, $b) {
return $a - $b;
}
/**
* Returns true if the queue is empty
*
* #return Boolean
*/
public function isEmpty() {
return sizeof($this->keys) === 0;
}
}
/lib/Maze/Reader.php
class Maze_Reader implements IteratorAggregate
{
/**
* The initial maze
* #var string
*/
protected $rawMaze;
/**
* A tow dimensional array holding the parsed maze
* #var array
*/
protected $map = array();
/**
* A flat array holding all maze nodes
* #var array
*/
protected $nodes = array();
/**
* A value map for easier access
* #var array
*/
protected $valueMap = array();
/**
* Constructs a maze reader
*
* #param string $file A path to a maze file
*/
public function __construct($file) {
$this->rawMaze = file_get_contents($file);
$this->parseMaze($this->rawMaze);
}
/**
* Parses the raw maze into usable Maze_Nodes
*
* #param string $maze
*/
protected function parseMaze($maze) {
foreach (explode("\n", $maze) as $y => $row) {
foreach (str_split(trim($row)) as $x => $cellValue) {
if (!isset($this->map[$x])) {
$this->map[$x] = array();
}
if (!isset($this->valueMap[$cellValue])) {
$this->valueMap[$cellValue] = array();
}
$this->nodes[] = new Maze_Node($x, $y, $cellValue, $this);;
$this->map[$x][$y] =& $this->nodes[sizeof($this->nodes) - 1];
$this->valueMap[$cellValue][] =& $this->nodes[sizeof($this->nodes) - 1];
}
}
}
/**
* Returns the neighobrs of $node
*
* #return array
*/
public function getNeighbors(Maze_Node $node) {
$result = array();
$top = $node->y() - 1;
$right = $node->x() + 1;
$bottom = $node->y() + 1;
$left = $node->x() - 1;
// top left
if (isset($this->map[$left], $this->map[$left][$top])) {
$result[] = $this->map[$left][$top];
}
// top center
if (isset($this->map[$node->x()], $this->map[$node->x()][$top])) {
$result[] = $this->map[$node->x()][$top];
}
// top right
if (isset($this->map[$right], $this->map[$right][$top])) {
$result[] = $this->map[$right][$top];
}
// right
if (isset($this->map[$right], $this->map[$right][$node->y()])) {
$result[] = $this->map[$right][$node->y()];
}
// bottom right
if (isset($this->map[$right], $this->map[$right][$bottom])) {
$result[] = $this->map[$right][$bottom];
}
// bottom center
if (isset($this->map[$node->x()], $this->map[$node->x()][$bottom])) {
$result[] = $this->map[$node->x()][$bottom];
}
// bottom left
if (isset($this->map[$left], $this->map[$left][$bottom])) {
$result[] = $this->map[$left][$bottom];
}
// left
if (isset($this->map[$left], $this->map[$left][$node->y()])) {
$result[] = $this->map[$left][$node->y()];
}
return $result;
}
/**
* #IteratorAggregate
*/
public function getIterator() {
return new ArrayIterator($this->nodes);
}
/**
* Returns a node by value
*
* #param mixed $value
* #param boolean $returnOne
* #param mixed $fallback
* #return mixed
*/
public function getByValue($value, $returnOne = false, $fallback = array()) {
$result = isset($this->valueMap[$value]) ? $this->valueMap[$value] : $fallback;
if ($returnOne && is_array($result)) {
$result = array_shift($result);
}
return $result;
}
/**
* Simple output
*/
public function __toString() {
$result = array();
foreach ($this->map as $x => $col) {
foreach ($col as $y => $node) {
$result[$y][$x] = (string)$node;
}
}
return implode("\n", array_map('implode', $result));
}
}
/lib/Maze/Node.php
class Maze_Node
{
protected $x;
protected $y;
protected $value;
protected $maze;
protected $g;
protected $predecessor;
/**
* #param Integer $x
* #param Integer $y
* #param mixed $value
* #param Maze_Reader $maze
*/
public function __construct($x, $y, $value, $maze) {
$this->x = $x;
$this->y = $y;
$this->value = $value;
$this->maze = $maze;
}
/**
* Getter for x
*
* #return Integer
*/
public function x() {
return $this->x;
}
/**
* Getter for y
*
* #return Integer
*/
public function y() {
return $this->y;
}
/**
* Setter/Getter for g
*
* #param mixed $g
* #return mixed
*/
public function g($g = null) {
if ($g !== null) {
$this->g = $g;
}
return $this->g;
}
/**
* Setter/Getter for value
*
* #param mixed $value
* #return mixed
*/
public function value($value = null) {
if ($value !== null) {
$this->value = $value;
}
return $this->value;
}
/**
* Setter/Getter for predecessor
*
* #param Maze_Node $predecessor
* #return Maze_Node|null
*/
public function predecessor(Maze_Node $predecessor = null) {
if ($predecessor !== null) {
$this->predecessor = $predecessor;
}
return $this->predecessor;
}
/**
* simple distance getter
*
* #param Maze_Node $that
* #return Float
*/
public function distance(Maze_Node $that) {
if ($that->value() === 'W') {
return PHP_INT_MAX;
}
return sqrt(pow($that->x() - $this->x, 2) + pow($that->y() - $this->y, 2));
}
/**
* Test for equality
*
* #param Maze_Node $that
* #return boolean
*/
public function equals(Maze_Node $that) {
return $this == $that;
}
/**
* Returns the successors of this node
*
* #return array
*/
public function successors() {
return $this->maze->getNeighbors($this);
}
/**
* For debugging
*
* #return string
*/
public function __toString() {
return (string)$this->value;
}
}

Reading a value from application.ini

I have a value that's defined in application.ini
conditions.time= 50
How can I read it in an zend action the zend way?
You can use Zend_Config_Ini
$config = new Zend_Config_Ini('my/ini/file.ini');
echo $config->conditions->time; // 50
Here is my approach, which you can use anywhere in the application:
class My_Controller_Action_Helper_Options extends Zend_Controller_Action_Helper_Abstract
{
/**
* Options separator delimiterm e.g.
* option.subkey or
* option/subkey
*/
const DELIMITER = '.';
/**
* Retrieve application options from bootstrap
*
* #return array
*/
public function getOptions()
{
$front = $this->getFrontController();
$bootstrap = $front->getParam('bootstrap');
if (null === $bootstrap) {
throw new Exception('Unable to find bootstrap');
}
return $bootstrap->getOptions();
}
/**
* Get array key if exists, otherwise returns null
*
* #param array $values
* #param string $key
* #return mixed
*/
private static function _getValue($values, $key)
{
if (is_array($values) && isset($values[$key])) {
return $values[$key];
}
return null;
}
/**
* Get application option form bootstrap
*
* #example
* $options = Zend_Controller_Action_HelperBroker::getStaticHelper('options')
* ->get('conditions.time', 'defaultvalue');
*
* #param string $section
* #param mixed $default
* #return Zend_Config
*/
public function get($section = null, $default = null)
{
$value = $this->getOptions();
if (null !== $section && is_string($section)) {
if (false === strpos($section, self::DELIMITER)) {
$value = $this->_getValue($value, $section);
} else {
$sections = explode(self::DELIMITER, $section);
foreach ($sections as $section) {
$value = $this->_getValue($value, $section);
if (null === $value) {
break;
}
}
}
}
if (null === $value) {
return $default;
}
return $value;
}
/**
* #param string $section
* #param mixed $default
* #return Zend_Config
*/
public function direct($section = null, $default = null)
{
return $this->get($section, $default);
}
}
The Application's Bootstrap.php has access to the application.ini using $this->getOptions(), you could store the value you want in your registry something like this:
public function _initConditions()
{
$config = $this->getOptions();
if (isset($config['conditions']))
{
$registry = Zend_Registry::getInstance();
$registry->conditions = $config['conditions'];
}
}
You could then access your conditions using the registry, in much the same way that you set them here.
Here is an action helper for that :
class My_Controller_Action_Helper_Config
extends Zend_Controller_Action_Helper_Abstract
{
/**
* #return array
*/
public function direct()
{
$bootstrap = $this->getActionController()->getInvokeArg('bootstrap');
$ns = strtolower(trim($bootstrap->getAppNamespace(), '_'));
return $bootstrap->getOption($ns);
}
}
You have to put your application namespace as a prefix :
; application.ini
My.conditions.time= 50
You can use it in a controller like this :
$config = $this->_helper->config();
$this->view->time = $config['conditions']['time'];
You might be able to use getenv('conditions.time')
http://www.php.net/manual/en/function.getenv.php
PHP has a parse_ini_file() function.

Fatal error: Declaration of registerContainerConfiguration must be compatible with that of Kernel::registerContainerConfiguration

Do anyone know why this occurs?
as far I can get, the child class method is declared in the same way as parent's.
Thanks!
here is my kernel code:
<?php
require_once __DIR__.'/../src/autoload.php';
use Symfony\Framework\Kernel;
use Symfony\Components\DependencyInjection\Loader\YamlFileLoader as ContainerLoader;
use Symfony\Components\Routing\Loader\YamlFileLoader as RoutingLoader;
use Symfony\Framework\KernelBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\ZendBundle\ZendBundle;
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;
use Symfony\Bundle\DoctrineBundle\DoctrineBundle;
use Symfony\Bundle\DoctrineMigrationsBundle\DoctrineMigrationsBundle;
use Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle;
use Symfony\Bundle\PropelBundle\PropelBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Application\UfaraBundle\UfaraBundle;
class UfaraKernel extends Kernel {
public function registerRootDir() {
return __DIR__;
}
public function registerBundles() {
$bundles = array(
new KernelBundle(),
new FrameworkBundle(),
new ZendBundle(),
new SwiftmailerBundle(),
new DoctrineBundle(),
//new DoctrineMigrationsBundle(),
//new DoctrineMongoDBBundle(),
//new PropelBundle(),
//new TwigBundle(),
new UfaraBundle(),
);
if ($this->isDebug()) {
}
return $bundles;
}
public function registerBundleDirs() {
$bundles = array(
'Application' => __DIR__.'/../src/Application',
'Bundle' => __DIR__.'/../src/Bundle',
'Symfony\\Framework' => __DIR__.'/../src/vendor/symfony/src/Symfony/Framework',
'Symfony\\Bundle' => __DIR__.'/../src/vendor/symfony/src/Symfony/Bundle',
);
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader) {
return $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
public function registerRoutes() {
$loader = new RoutingLoader($this->getBundleDirs());
return $loader->load(__DIR__.'/config/routing.yml');
}
}
here is the parent class code:
<?php
namespace Symfony\Framework;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Resource\FileResource;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Loader\DelegatingLoader;
use Symfony\Component\DependencyInjection\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Framework\ClassCollectionLoader;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier#symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* The Kernel is the heart of the Symfony system. It manages an environment
* that can host bundles.
*
* #author Fabien Potencier <fabien.potencier#symfony-project.org>
*/
abstract class Kernel implements HttpKernelInterface, \Serializable
{
protected $bundles;
protected $bundleDirs;
protected $container;
protected $rootDir;
protected $environment;
protected $debug;
protected $booted;
protected $name;
protected $startTime;
protected $request;
const VERSION = '2.0.0-DEV';
/**
* Constructor.
*
* #param string $environment The environment
* #param Boolean $debug Whether to enable debugging or not
*/
public function __construct($environment, $debug)
{
$this->environment = $environment;
$this->debug = (Boolean) $debug;
$this->booted = false;
$this->rootDir = realpath($this->registerRootDir());
$this->name = basename($this->rootDir);
if ($this->debug) {
ini_set('display_errors', 1);
error_reporting(-1);
$this->startTime = microtime(true);
} else {
ini_set('display_errors', 0);
}
}
public function __clone()
{
if ($this->debug) {
$this->startTime = microtime(true);
}
$this->booted = false;
$this->container = null;
$this->request = null;
}
abstract public function registerRootDir();
abstract public function registerBundles();
abstract public function registerBundleDirs();
abstract public function registerContainerConfiguration(LoaderInterface $loader);
/**
* Checks whether the current kernel has been booted or not.
*
* #return boolean $booted
*/
public function isBooted()
{
return $this->booted;
}
/**
* Boots the current kernel.
*
* This method boots the bundles, which MUST set
* the DI container.
*
* #throws \LogicException When the Kernel is already booted
*/
public function boot()
{
if (true === $this->booted) {
throw new \LogicException('The kernel is already booted.');
}
if (!$this->isDebug()) {
require_once __DIR__.'/bootstrap.php';
}
$this->bundles = $this->registerBundles();
$this->bundleDirs = $this->registerBundleDirs();
$this->container = $this->initializeContainer();
// load core classes
ClassCollectionLoader::load(
$this->container->getParameter('kernel.compiled_classes'),
$this->container->getParameter('kernel.cache_dir'),
'classes',
$this->container->getParameter('kernel.debug'),
true
);
foreach ($this->bundles as $bundle) {
$bundle->setContainer($this->container);
$bundle->boot();
}
$this->booted = true;
}
/**
* Shutdowns the kernel.
*
* This method is mainly useful when doing functional testing.
*/
public function shutdown()
{
$this->booted = false;
foreach ($this->bundles as $bundle) {
$bundle->shutdown();
$bundle->setContainer(null);
}
$this->container = null;
}
/**
* Reboots the kernel.
*
* This method is mainly useful when doing functional testing.
*
* It is a shortcut for the call to shutdown() and boot().
*/
public function reboot()
{
$this->shutdown();
$this->boot();
}
/**
* Gets the Request instance associated with the master request.
*
* #return Request A Request instance
*/
public function getRequest()
{
return $this->request;
}
/**
* Handles a request to convert it to a response by calling the HttpKernel service.
*
* #param Request $request A Request instance
* #param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* #param Boolean $raw Whether to catch exceptions or not
*
* #return Response $response A Response instance
*/
public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
{
if (false === $this->booted) {
$this->boot();
}
if (null === $request) {
$request = $this->container->get('request');
} else {
$this->container->set('request', $request);
}
if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->request = $request;
}
$response = $this->container->getHttpKernelService()->handle($request, $type, $raw);
$this->container->set('request', $this->request);
return $response;
}
/**
* Gets the directories where bundles can be stored.
*
* #return array An array of directories where bundles can be stored
*/
public function getBundleDirs()
{
return $this->bundleDirs;
}
/**
* Gets the registered bundle names.
*
* #return array An array of registered bundle names
*/
public function getBundles()
{
return $this->bundles;
}
/**
* Checks if a given class name belongs to an active bundle.
*
* #param string $class A class name
*
* #return Boolean true if the class belongs to an active bundle, false otherwise
*/
public function isClassInActiveBundle($class)
{
foreach ($this->bundles as $bundle) {
$bundleClass = get_class($bundle);
if (0 === strpos($class, substr($bundleClass, 0, strrpos($bundleClass, '\\')))) {
return true;
}
}
return false;
}
/**
* Returns the Bundle name for a given class.
*
* #param string $class A class name
*
* #return string The Bundle name or null if the class does not belongs to a bundle
*/
public function getBundleForClass($class)
{
$namespace = substr($class, 0, strrpos($class, '\\'));
foreach (array_keys($this->getBundleDirs()) as $prefix) {
if (0 === $pos = strpos($namespace, $prefix)) {
return substr($namespace, strlen($prefix) + 1, strpos($class, 'Bundle\\') + 7);
}
}
}
public function getName()
{
return $this->name;
}
public function getSafeName()
{
return preg_replace('/[^a-zA-Z0-9_]+/', '', $this->name);
}
public function getEnvironment()
{
return $this->environment;
}
public function isDebug()
{
return $this->debug;
}
public function getRootDir()
{
return $this->rootDir;
}
public function getContainer()
{
return $this->container;
}
public function getStartTime()
{
return $this->debug ? $this->startTime : -INF;
}
public function getCacheDir()
{
return $this->rootDir.'/cache/'.$this->environment;
}
public function getLogDir()
{
return $this->rootDir.'/logs';
}
protected function initializeContainer()
{
$class = $this->getSafeName().ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
$location = $this->getCacheDir().'/'.$class;
$reload = $this->debug ? $this->needsReload($class, $location) : false;
if ($reload || !file_exists($location.'.php')) {
$this->buildContainer($class, $location.'.php');
}
require_once $location.'.php';
$container = new $class();
$container->set('kernel', $this);
return $container;
}
public function getKernelParameters()
{
$bundles = array();
foreach ($this->bundles as $bundle) {
$bundles[] = get_class($bundle);
}
return array_merge(
array(
'kernel.root_dir' => $this->rootDir,
'kernel.environment' => $this->environment,
'kernel.debug' => $this->debug,
'kernel.name' => $this->name,
'kernel.cache_dir' => $this->getCacheDir(),
'kernel.logs_dir' => $this->getLogDir(),
'kernel.bundle_dirs' => $this->bundleDirs,
'kernel.bundles' => $bundles,
'kernel.charset' => 'UTF-8',
'kernel.compiled_classes' => array(),
),
$this->getEnvParameters()
);
}
protected function getEnvParameters()
{
$parameters = array();
foreach ($_SERVER as $key => $value) {
if ('SYMFONY__' === substr($key, 0, 9)) {
$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
}
}
return $parameters;
}
protected function needsReload($class, $location)
{
if (!file_exists($location.'.meta') || !file_exists($location.'.php')) {
return true;
}
$meta = unserialize(file_get_contents($location.'.meta'));
$time = filemtime($location.'.php');
foreach ($meta as $resource) {
if (!$resource->isUptodate($time)) {
return true;
}
}
return false;
}
protected function buildContainer($class, $file)
{
$parameterBag = new ParameterBag($this->getKernelParameters());
$container = new ContainerBuilder($parameterBag);
foreach ($this->bundles as $bundle) {
$bundle->registerExtensions($container);
if ($this->debug) {
$container->addObjectResource($bundle);
}
}
if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
$container->merge($cont);
}
$container->freeze();
foreach (array('cache', 'logs') as $name) {
$dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
if (!is_dir($dir)) {
if (false === #mkdir($dir, 0777, true)) {
die(sprintf('Unable to create the %s directory (%s)', $name, dirname($dir)));
}
} elseif (!is_writable($dir)) {
die(sprintf('Unable to write in the %s directory (%s)', $name, $dir));
}
}
// cache the container
$dumper = new PhpDumper($container);
$content = $dumper->dump(array('class' => $class));
if (!$this->debug) {
$content = self::stripComments($content);
}
$this->writeCacheFile($file, $content);
if ($this->debug) {
$container->addObjectResource($this);
// save the resources
$this->writeCacheFile($this->getCacheDir().'/'.$class.'.meta', serialize($container->getResources()));
}
}
protected function getContainerLoader(ContainerInterface $container)
{
$resolver = new LoaderResolver(array(
new XmlFileLoader($container, $this->getBundleDirs()),
new YamlFileLoader($container, $this->getBundleDirs()),
new IniFileLoader($container, $this->getBundleDirs()),
new PhpFileLoader($container, $this->getBundleDirs()),
new ClosureLoader($container),
));
return new DelegatingLoader($resolver);
}
/**
* Removes comments from a PHP source string.
*
* We don't use the PHP php_strip_whitespace() function
* as we want the content to be readable and well-formatted.
*
* #param string $source A PHP string
*
* #return string The PHP string with the comments removed
*/
static public function stripComments($source)
{
if (!function_exists('token_get_all')) {
return $source;
}
$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= $token[1];
}
}
// replace multiple new lines with a single newline
$output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
// reformat {} "a la python"
$output = preg_replace(array('/\n\s*\{/', '/\n\s*\}/'), array(' {', ' }'), $output);
return $output;
}
protected function writeCacheFile($file, $content)
{
$tmpFile = tempnam(dirname($file), basename($file));
if (false !== #file_put_contents($tmpFile, $content) && #rename($tmpFile, $file)) {
chmod($file, 0644);
return;
}
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
}
public function serialize()
{
return serialize(array($this->environment, $this->debug));
}
public function unserialize($data)
{
list($environment, $debug) = unserialize($data);
$this->__construct($environment, $debug);
}
}
Your answer lies in the imported namespaces. In the Kernel's file, there's this use clause:
use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
So that ties LoaderInterface to the fully namespaced class Symfony\Component\DependencyInjection\Loader\LoaderInterface.
Basically making the signature:
public function registerContainerConfiguration(Symfony\Component\DependencyInjection\Loader\LoaderInterface $loader);
In your class, you don't import that namespace. So PHP by default assumes the class is in your namespace (since none of the imported namespaces have that interface name).
So your signature is (since you don't declare a namespace):
public function registerContainerConfiguration(\LoaderInterface $loader);
So to get them to match, simply add the use line to the top of your file:
use Symfony\Component\DependencyInjection\Loader\LoaderInterface;

Categories