Doctrine generate entity aborted - php

When I try to execute the command
php bin\console doctrine:generate:entity
I put the name of my bundle, the type of configuration that I want, in my case yml, but at the moment of insert the name of the first attribute for the entity the console starts to out the message
New field name (press <return> to stop adding fields): Aborted
Until I don't use the combination ctrl+c the console don't stop of print this message.
There is a question with the same problem here:
Doctrines automatically aborted in Symfony.
In that question someone says that is problem of stty and stdin but nobody explain how solve it.
Update:
In Symfony there are two functions that return a RuntimeException with the message Aborted. Both are in the QuesionHelper class. The first function is getHiddenResponse and the second is readFromInput. I suppose that the second function is returning the error but I don't know why, I have not yet been able to debug it but I think that the options are:
The condition of the if return false and the function fgets return false too. In this case can be that the variable $stream isn't equals to STDIN or that the function readline don't exists
The function readLine doesn't works.
This is the code of the readFromInput function:
private function readFromInput($stream)
{
if (STDIN === $stream && function_exists('readline')) {
$ret = readline();
} else {
$ret = fgets($stream, 4096);
}
if (false === $ret) {
throw new RuntimeException('Aborted');
}
return trim($ret);
}

Related

Execute a node script in Laravel

I need to run a node script inside my Laravel application.
I've tried using the Symfony Process, but it kill the process after the PHP execution and I need to keep the node process running, so my next step was try to use the shell_exec and exec functions. It's worked nice in the tinker and in Tinkerwell, but when I try to use inside the Laravel class it gives the following error 500:
production.ERROR: Call to undefined function shell_exec() {"exception":"[object] (Error(code: 0): Call to undefined function shell_exec() at ...
As it is a VPS I set my php.ini to disable_functions= but it still not working inside Laravel, only if I use it outside the framework.
Thanks in advance and sorry for my bad English.
Unfortunately this can't be done with exec() (nor system() or shell_exec()).
But if you install the pcntl extension, define something like:
<?php
function detached_exec($cmd) {
$pid = pcntl_fork();
if ($pid == 0) {
// Childe process main-loop goes here.
posix_setsid();
exec($cmd);
exit(0);
} else if ($pid == -1) {
// Process fork failed.
return FALSE;
}
return $pid;
}
And use like:
// Edit this:
$cmd = 'node --version';
$pid = detached_exec($cmd);
if($pid === FALSE) {
echo 'exec failed';
}
I ended giving up. I've developed an workaround.
Instead calling the node file by PHP, I made a node script whit express who receives a POST request and start the code.
Now I can put my node code in another server and it will keep running with no problems and make my system scalable.

How to retrieve the result of a Firebird INSERT ... RETURNING in Yii

I am trying to use Firebird 2.1 with Yii (using plugin http://www.yiiframework.com/extension/yii2-firebird/) but I have problems doing insert (save) commands, the error message is:
SQLSTATE[HY000]: General error: -502 Cursor is not open
I have found that Yii generates insert statement with returning clause, e.g.:
INSERT INTO CONTRACTS (contract_no) VALUES (10002) RETURNING contract_no
And I guess that the problem is the following: Yii tries to read results from this insert command but there are problems with Yii-Firebird plugin which closes cursors immediately after pdoStatement->execute. The exception is generated in yii/db/Command.php file function protected function queryInternal($method, $fetchMode = null) whose code reads:
$this->pdoStatement->execute();
if ($method === '') {
$result = new DataReader($this);
} else {
if ($fetchMode === null) {
$fetchMode = $this->fetchMode;
}
try {
$result = call_user_func_array([$this->pdoStatement, $method], (array) $fetchMode);
$this->pdoStatement->closeCursor();
} catch (Exception $ex) {
Yii::trace('Fetch error', 'yii\db\Command::query');
}
}
I have the following questions:
Does anyone uses Firebird with Yii, what the experience is?
How to explain code:
call_user_func_array([$this->pdoStatement, $method], (array) $fetchMode);
and where to correct it? I guess that this method should be overriden in Yii-Firebird plugin with the aim to check whether the cursor is open and, if necessary, open the cursor?
Answer for the first question.
With the YII2 I use "edgardmessias/yii2-firebird": "^0.7.1", extension for Firebird 2.1.1883.
I found that it better to work not with ActiveDataProvider, but with
$dataProvider = new ArrayDataProvider([
in the SearchModel. That way are not so much errors while searching with relation.
But main problem is case sensitive search. I always needed to write in the search field same letters what record have, Big or small.

Return (terminate) caller function from callee

I would like to create a forward-function that terminates the caller-function.
Currently I'm using "exit" in the callee-function to achieve this.
function caller (){
if($condition){
forward();
}
//code that is not executet if $condition is true
}
//current solution
function forward(){
//do something
exit;
}
I would like to omit the return-statement after calling the forward-function.
I guess there is no other way except throwing an exception?
Thanks for your help.
There are several ways:
1) Enclosing the code after if info else branch
2) As you mentioned, throwing an exception
3) or plainly doing:
function caller (){
if($condition){
forward();
return; // you return nothing (void, in PHP's case that would be NULL, I believe)
}
//code that is not executet if $condition is true
}
Throwing exceptions, using exit or some other nasty stuff is overkill no matter how you look at it (if all you want to do is conditional execution of some lines of code).
Solution 1
Use an else statement. This function will implicitly return NULL (in PHP, by default, ommiting the return statement when exiting a function, will return NULL to the caller).
function caller (){
if($condition){
forward();
}
else
{
//code that is not executet if $condition is true
}
}
Solution 2
The return statement will exit the current function.
function caller (){
if($condition){
forward();
return; //this function exits, and no other lines inside this function are executed
}
//code that is not executet if $condition is true
}
Don't use exit to terminate scripts, without passing and exit code. Exit is just like die, except instead of outputting to php://stdout (same thing as what echo does) it will return an exit code to the OS (exit is usefull only in CLI (command line) mode).
You can't terminate a "caller" function before the "called" function terminate itself.
There is a stack of calls that is allocated and return pointer issues.
I suppose that the best way to terminate a function itself is .... just terminate itself.
So
function caller (){
if($condition){
return 0;
}
//code that is not executet if $condition is true
}
I've only seen this answer in the comments, but not in any of the other answers.
To wrap up, let's consider two approaches and why they're not suitable for a forward functionality:
Use exceptions; exceptions should not be used for flow control, they're meant to handle exceptional events (when an operation has failed).
Use goto; this statement can only be used to jump within the same block of execution, which in this case would be inside the caller and the only jump target is at the end of the function.
Use exit(); this will terminate the script altogether and doesn't give any of the underlying code a chance to do anything else. It's typically used for fatal errors of some sort, or in rare cases whereby you set a few HTTP headers and then prevent any more output from being sent to the browser.
Technically speaking, a forward call should basically be used to express "say whatever he says"; and there's a perfect solution for that:
return forward();
It delivers the result of the forward() directly to the code that called the caller() while still respecting the stack execution order of your application.
Possibly, "goto" is the only solution:
function post_to_host($url, $data, $cook, $ref, &$resp_head, $type=1)
{$GLOBALS['POST_TO_HOST.TIME_START']=time();
...
stream_set_timeout($fp, 20);
fputs($fp, "Accept: */*\r\n"); if (check_timeout()) {goto timeout;}
fputs($fp, "Accept-Language: *\r\n"); if (check_timeout()) {goto timeout;}
fputs($fp, "Accept-Encoding: *\r\n"); if (check_timeout()) {goto timeout;}
while (!feof($fp))
{$line.=fgets($fp); if (check_timeout()) {goto timeout;}
}
return $line;
timeout:
{return 'POST EXCEPTION: Total timeout!';
}
}
function check_timeout()
{return time()-$GLOBALS['POST_TO_HOST.TIME_START']>60;
}

Selenium RC: How to check if an element has a given attribute?

I have some buttons with an onclick attribute and some that don't. I want to check if the specified element has the onclick attribute. How can I do this?
getAttribute() returns the attribute value when it has one. When it doesn't, it throws a RuntimeException and stops the test (even when I wrap it in a try/catch block).
$onclickValue = $this->getAttribute("$locator#onclick"); //works when the attribute exists
By using getEval(), you can execute the javascript hasAttribute() function. Using findElement(), will allow you to work with any type of locator pattern.
$hasAttribute = $this->getEval('this.browserbot.findElement("' . $locator . '").hasAttribute("onclick")');
if ($hasAttribute === 'true') {
//attribute exists
}
Note that getEval() returns a string, not a boolean.
You could first check if the element is present using XPath //location/of/element[#onclick]
Please excuse me if this does not apply to Selenium RC. In Selenium IDE, one can use the
assertElementNotPresent
command, which (despite the name) can determine whether a given attribute is present. It's only parameter is an element locator that can be of the form
element-id#attribute.
Of course this will only be appropriate if you know which elements should have the attribute in question. If not then I guess you'll have to iterate through element sets using XPath expressions.
The reason why a RuntimeException is thrown is because the way PHPUnit's selenium driver works.
It considers certain situations as errors that perform a stop() of the test execution. In particular, the code that stops the test in that situation is the following:
protected function getString($command, array $arguments)
{
try {
$result = $this->doCommand($command, $arguments);
}
catch (RuntimeException $e) {
$this->stop();
throw $e;
}
return (strlen($result) > 3) ? substr($result, 3) : '';
}
I already opened an issue regarding this way of handling errors in the driver at https://github.com/sebastianbergmann/phpunit/issues/276
BTW, removing the calls to stop() in both doCommand() and getString() of /usr/share/php/PHPUnit/Extensions/SeleniumTestCase/Driver.php will make your code able of catching the exception and handling it as you prefer.

Cheking and error on a PHP function

There is a way to check with a "IF" if a function fails in php?
Ex.
If (getimagesize($image) returns and error) {
echo 'Error: function dont work';
}
else
{
// do something
}
Thanks!
I 'm assuming you are interested specifically in a function like getimagesize, which does not return any error codes and makes it hard on us. But not impossible.
The documentation for getimagesize states:
If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.
Therefore, you need to do two things:
Get notified of the error and act upon it somehow
Have the error not be displayed or otherwise affect the execution of your program at all (after all, your are going to be taking care of any errors with error-handling code yourself)
You can achieve the first one using set_error_handler() and restore_error_handler(). You can achieve the second with the error-control operator #.
So, the code must go something like this:
// Set our own error handler; we will restore the default one afterwards.
// Our new error handler need only handle E_WARNING and E_NOTICE, as per
// the documentation of getimagesize().
set_error_handler("my_error_handler", E_WARNING | E_NOTICE);
// No error has occured yet; it is the responsibility of my_error_handler
// to set $error_occurred to true if an error occurs.
$error_occurred = false;
// Call getimagesize; use operator # to have errors not be generated
// However, your error handler WILL STILL BE CALLED, as the documentation
// for set_error_handler() states.
$size = #getimagesize(...);
// At this point, my_error_handler will have run if an error occurred, and
// $error_occurred will be true. Before doing anything with it, restore the
// previous error handler
restore_error_handler();
if($error_occurred) {
// whatever
}
else {
// no error; $size holds information we can use
}
function my_error_handler($errno, $errstr, $file, $line) {
global $error_occurred;
// If the code is written as above, then we KNOW that an error
// here was caused by getimagesize(). We also know what error it was:
switch($errno) {
case E_WARNING: // Access to image impossible, or not a valid picture
case E_NOTICE: // Read error
}
// We could also check what $file is and maybe do something based on that,
// if this error handler is used from multiple places. However, I would not
// recommend that. If you need more functionality, just package all of this
// into a class and use the objects of that class to store more state.
$error_occurred = true;
return true; // Do not let PHP's default error handler handle this after us
}
Of course, this is not very maintainable (you have a global variable $error_occurred there, and this is not a good practice). So for a solution which not only works but is also nicely engineered, you would package all this in a class. That class would define:
A method which implements the error handler (my_error_handler in the above example). To set an object method as an error handler instead of a global function, you need to call set_error_handler with a suitable first parameter; see the documentation for callback.
A method which lets the class set the error handler, execute some code of your choice, save the "error happened while executing your code" flag, and restore the error handler. This method could e.g. take a callback provided by your calling code and an array of parameters and use call_user_func_array to execute it. If, during execution, the error handler set from #1 above is called, mark this in a variable in your object. Your method would return the return value of call_user_func_array to the calling code.
A method or variable which the calling code can use to access the result from #2 above.
So then, if that class is called ErrorWatcher, your calling code would be something like:
$watcher = new ErrorWatcher;
$size = $watcher->watch("getimagesize",
array( /* params for getimagesize here */ ));
// $size holds your result, if an error did not occur;
// check for errors and we 're done!
switch($watcher->report_last_error()) {
// error handling logic here
}
...which is nice and neat and does not mess with global variables. I hope I explained this well enough to enable you to write class ErrorWatcher yourself. :-)
Take a look at exceptions. You'll have to throw them in your function though.
Edit: By the way, if your function is not supposed to return a boolean, you can always have it return false if something goes wrong and check like:
$result = getimagesize($image);
if ($result === false)
{
//
}
Whatever the condition is on an "if" statement (what's inside the parenthesis) will return "true" or "false".
If the condition returns "true", the first statement will be executed, otherwise the second statement will get executed.
You can put this, error_reporting(E_ALL); at the very top of your script, right after your opening php tag, to see what error you get.
Hope it helps.
Maybe something like this:
<?php
error_reporting(E_ALL);
more stuff here ...
if(your condition here){
echo "condition is true, do something";
}else{
echo "condition is not true, do something else";
}
if the function or statement you run inside the "if" or "else" fails, at least you know which one was it based on the result, and the error_reporting might tell you why it failed.

Categories