Zend_Translate, set up the log via the application resource plugin? - zend-framework

I'm using application resource plugins in a .ini file to set up my Zend_Translate with this code:
resources.translate.data = APPLICATION_PATH "/../languages"
resources.translate.adapter = "gettext"
resources.translate.options.scan = "directory"
Now I would like to add the log functionality to the translate, which in bootstrap I would do like this:
$writer = new Zend_Log_Writer_Stream( APPLICATION_PATH . '/../logs/translate.log');
$log = new Zend_Log($writer);
$translate->setOptions(
array(
'log' => $log,
'logUntranslated' => true
)
);
2 questions about this:
First, is it possible to this in the .ini file?
Secondly, it is possible to "extend" resource settings in the bootstrap? In other words, could I add for example this log option in the bootstrap to the translate while maintaining the other settings already made in the .ini file?

Alright, I haven't found a solution to this in the ini file, but I have found a way to "extend" my settings from the ini file in the bootstrap without overwriting them. I managed to do that like this:
protected function _initTranslate()
{
$writer = new Zend_Log_Writer_Stream( APPLICATION_PATH . '/../somedir/somefile.log');
$log = new Zend_Log($writer);
// get the translate resource from the ini file and fire it up
$resource = $this->getPluginResource('translate');
$translate = $resource->getTranslate();
// add the log to the translate
$translate->setOptions(
array(
'log' => $log,
'logUntranslated' => true
)
);
// return the translate to get it in the registry and so on
return $translate;
}
This works just fine. I'm going to remove the translate from the .ini though because I'm switching to my own adapter and don't know (yet) how to pull that off from the ini.

Related

Send file to Restful service in codeception

I would like to test restful API test for file uploading.
I try to run:
$I->sendPOST($this->endpoint, $postData, ['file' => 'example.jpg']);
and I would like it to behave the same as user sent example.jpg file in file input with name file but it doesn't seem to work this way. I'm getting:
[PHPUnit_Framework_ExceptionWrapper] An uploaded file must be an array or an instance of UploadedFile.
Is it possible to upload file using REST plugin in codeception? Documentation is very limited and it's hard to say how to do it.
I'm also testing API using Postman plugin to Google Chrome and I can upload file without a problem using this plugin.
I was struggling with the same problem recently and found out that there is another way to solve the issue without using Symfony's UploadedFile class. You only need to pass the array with file data in the same format as if it were the $_FILES array. For example, this code works perfectly well for me:
$I->sendPOST(
'/my-awesome-api',
[
'sample-field' => 'sample-value',
],
[
'myFile' => [
'name' => 'myFile.jpg',
'type' => 'image/jpeg',
'error' => UPLOAD_ERR_OK,
'size' => filesize(codecept_data_dir('myFile.jpg')),
'tmp_name' => codecept_data_dir('myFile.jpg'),
]
]
);
Hope this helps someone and prevents from inspecting the framework's source code (which I was forced to do, as the docs skip such an important detail)
After testing it seems to make it work we need to use UploadedFile object as file.
For example:
$path = codecept_data_dir();
$filename = 'example-image.jpg';
// copy original test file to have at the same place after test
copy($path . 'example.jpg', $path . $filename);
$mime = 'image/jpeg';
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile($path . $filename, $filename, $mime,
filesize($path . $filename));
$I->sendPOST($this->endpoint, $postData, ['file' => $uploadedFile]);
['file' => 'example.jpg'] format works too, but the value must be a correct path to existing file.
$path = codecept_data_dir();
$filename = 'example-image.jpg';
// copy original test file to have at the same place after test
copy($path . 'example.jpg', $path . $filename);
$I->sendPOST($this->endpoint, $postData, ['file' => $path . $filename]);
The below worked for myself,
On server:
$uploadedResume= $_FILES['resume_uploader'];
$outPut = [];
if (isset($uploadedResume) && empty($uploadedResume['error'])) {
$uploadDirectory = 'uploads/users/' . $userId . '/documents/';
if (!is_dir($uploadDirectory)) {
#mkdir($uploadDirectory, 0777, true);
}
$ext = explode('.', basename($uploadedResume['name']));
$targetPath = $uploadDirectory . md5(uniqid()) . '.' . end($ext);
if (move_uploaded_file($uploadedResume['tmp_name'], $targetPath)) {
$outPut[] = ['success' => 'success', 'uploaded_path' => $targetPath];
}
}
return json_encode($output);
Sorry for the long description code :P
On Testing side:
//resume.pdf is copied in to tests/_data directory
$I->sendPOST('/student/resume', [], ['resume_uploader' => codecept_data_dir('resume.pdf') ]);
#Yaronius's answer worked for me after I removed the following header from my test:
$I->haveHttpHeader('Content-Type', 'multipart/form-data');

How to use extensions in Zend Framework Routing

I'd like to use an extension like .htm in my URLs. Is there a way to accomplish that?
I've defined the following rule:
frontend.route = '/:standort/:id'
When I use the following
frontend.route = '/:standort/:id.htm'
then the name of the variable is id.htm like in $params["id.htm"].
How can I tell the Zend Framework what to use as variables?
Greetings
//Edit
my full config looks like this now:
frontend.type = 'Zend_Controller_Router_Route_Regex'
frontend.route = '/(.?)/(\w+?\.htm)'
frontend.defaults.module = 'frontend'
frontend.defaults.controller = 'index'
frontend.defaults.action = 'index'
frontend.defaults.standort = 'national'
frontend.map.1 = 'standort'
frontend.map.2 = 'id'
this is how I load the config
$file = APPLICATION_PATH . '/modules/' . $module . '/configs/routing.ini';
if(Zend_Loader::isReadable($file)){
$config = new Zend_Config_Ini($file);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config);
}
You can do this with regex routes:
frontend.type = "Zend_Controller_Router_Route_Regex"
frontend.route = '/(.?)/(\w+?\.htm)'
frontend.map.1 = "standort"
frontend.map.2 = "id"
but unless you're trying to preserve an existing URL structure, I'd just leave the .htm out - it serves no purpose.

zfdatagrid - Plugin by name 'Table' was not found in the registry;

I've just installed the library and tried a simple table with an array
$grid = Bvb_Grid::factory("Bvb_Grid_Deploy_Table");
$grid->setSource(new Bvb_Grid_Source_Array($this->pkg));
$myGrid = $grid->deploy();
I get this error:
Plugin by name 'Table' was not found in the registry; used paths: Bvb_Grid_Template_: Bvb/Grid/Template/
I'm not familiar with how this works, but would guess looking at the error that it looks in the Bvb/Grid/Template/ directory, there is a Table.php in there with the class Bvb_Grid_Template_Table.Thank you.
Try a little bit different. Write "Table" instead of "Bvb_Grid_Deploy_Table". In my example there is also grid configuration file...
$config = new Zend_Config_Ini(dirname(__FILE__) . '/../configs/grid.ini','clients');
$grid = Bvb_Grid::factory('Table', $config);
$model = new Sand_Model_DbTable_Asset();
$source = new Bvb_Grid_Source_Zend_Table($model);
$grid->setSource($source);
/*
more code here...
*/
$myGrid = $grid->deploy();
$this->view->grid = $myGrid;

addFilter Rename ZendFramework

i need to Rename a file with Zend_File_Transfer() only if new file match with old one in the server using some convention like newfile-1.ext where the -1 is the string that is added but Rename filter is strange, i really dont understand so good.
For example, is necesary some like this:
if(file_exists($file)){
$upload->addFilter('Rename', $file);
}
or Rename does it?
thanks
Here is an example from one of my apps. File is recvieved by an Zend_Form
$upload->receive();
$name = $upload->getFileName();
$newFile = 'mynewfile.xyz'
$filterFileRename = new Zend_Filter_File_Rename(array(
'target' => $this->path . $newFile, // path to file
'overwrite' => true
));
$file = $filterFileRename->filter($name);

ZEND tralslations for addMultiOption text in Form for poEdit

I dont have an idea why translations are not working in with Zend_Form.
I would like to be able translate options for selects.
For now i have something like this:
my form class:
(...)
$this->translate = Zend_Registry::get('translate');
Zend_Form::setDefaultTranslator( Zend_Registry::get('translate') );
(...)
$select = new Zend_Form_Element_Select('select');
// $select->addMultiOption('0', $this->translate('Aktywny'));
$select->addMultiOption('0', $this->translate->_('Aktywny'));
$select->addMultiOption('1', 'Nieaktywny');
in my bootstrap file i have something like this:
protected function _initTranslate()
{
Zend_Loader::loadClass('Zend_Translate');
Zend_Loader::loadClass('Zend_Registry');
$translate = new Zend_Translate('gettext', APPLICATION_PATH.'/languages',
'browser',
array('scan' => Zend_Translate::LOCALE_FILENAME));
//changing language and setting it to session if changed
$session = new Zend_Session_Namespace('jezyk');
if(isset($session->language)) {
$translate->setLocale($session->language);
} else
$translate->setLocale('pl');
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}
and it works fine for controllers, phtml files and plugins where i call it by
$this->translate('string to translate');
and in plugins
$this->view->translate('string to translate');
but those methods won't work in form. It throws exception:
Warning: Exception caught by form: No entry is registered for key 'translate' Stack Trace: #0
to make it working as i wrote in comment just have to change line:
$this->translate = Zend_Registry::get('translate');
for
$this->translate = Zend_Registry::get('Zend_Translate');
cause i didn't saw that i'm getting wrong translate from registry. It should be Zend_Translate like in Bootstrap file, not translate as i did.
And this is solution for my problems with translate and now i can make translations in form files :)