Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

dataAvailable method backported to 2.4 #1098

Closed
wants to merge 7 commits into from

4 participants

@rikvdh

I already did this for 3.0 earlier, now for 2.4

lib/Cake/Console/ConsoleInput.php
@@ -48,4 +48,16 @@ public function read() {
return fgets($this->_input);
}
+/**
+ * Check if data is available on stdin
+ *
+ * @param integer $timeout An optional time to wait for data
+ * @return bool True for data available, false otherwise
+ */
+ public function dataAvailable($timeout = 0) {
+ $readFds = array($this->_input);
+ $readyFds = stream_select($readFds, $w = null, $e = null, $timeout);
@markstory Owner

Inline assignments in function calls always look like a mistake. You should assign these variables above the function call and pass them in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rikvdh

@markstory See pull request #1020 ;-)

@dereuromark
Collaborator

I don't think the initialization is even necessary.
In PHP you can just do stream_select($readFds, $writeFds, $errorFds, $timeout);.
$writeFds, $errorFds will automatically be initialized here as they are merely return variables.

A (small) flaw in PHP itself one could say.

@markstory
Owner

Well I made a mistake last time, these things happen. I'll fix the code in 3.0.

@rikvdh

I think it the initialization is necessary because these parameters are also a input to this function, by passing uninitialized variables you don't actually know what happens and PHP throws a warning.

From: http://php.net/manual/en/function.stream-select.php

e.g. write: 'The streams listed in the write array will be watched to see if a write will not block.'

which means that (even though its a reference) it still is input.

@dereuromark
Collaborator

Interesting, I only know if from exec() etc where you don't need to do that. But maybe this method behaves differently.

@markstory
Owner

Based on my local testing, this method like most of the gross C API's in PHP don't need the variables initialized.

<?php
error_reporting(-1);
$fds = array(fopen('php://stdin', 'r'));
$ready = stream_select($fds, $write, $error, 0);

Works fine on my machine without any notice errors.

@rikvdh

I still don't like it, when the PHP interpreter changes this may give problems, just relying on the default of the interpreter is not the best way to go in my opinion.

@dereuromark
Collaborator

This behavior is quite unlikely to ever change :)

@rikvdh

@dereuromark, Yeah I think your right. Still I think its better to init imo. BUT now we saved 2 lines of code!

@dereuromark
Collaborator

You still need to squash, though.

@rikvdh

@dereuromark what do you mean?

@dereuromark
Collaborator

those 5 commits should be merged into a single one by rebasing it upstream and squashing them into a single commit.

@ceeram
Collaborator

@markstory except that phpcs will complain about Unused variable.

@markstory
Owner

@ceeram That's a separate problem, I can take care of that after the merge.

Rik van der ... added some commits
Rik van der Heijden Backport dataAvailable to 2.4 51fdfc6
Rik van der Heijden Merge branch '2.4' of github.com:djbobke/cakephp into 2.4
* '2.4' of github.com:djbobke/cakephp:
  Remove variable initialization
537a144
@rikvdh

See #1112 :)

@markstory
Owner

See #1112

@markstory markstory closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2013
  1. dataAvailable method backported to 2.4

    Rik van der Heijden authored
  2. Added Router::reload for CrudAuthorizeTest

    Rik van der Heijden authored
  3. Removed Router::reload for CrudAuthorizeTest, oops :)

    Rik van der Heijden authored
Commits on Jan 30, 2013
  1. Fix separate variables for dataAvailable

    Rik van der Heijden authored
  2. Remove variable initialization

    Rik van der Heijden authored
Commits on Feb 2, 2013
  1. Backport dataAvailable to 2.4

    Rik van der Heijden authored
  2. Merge branch '2.4' of github.com:djbobke/cakephp into 2.4

    Rik van der Heijden authored
    * '2.4' of github.com:djbobke/cakephp:
      Remove variable initialization
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +12 −0 lib/Cake/Console/ConsoleInput.php
View
12 lib/Cake/Console/ConsoleInput.php
@@ -48,4 +48,16 @@ public function read() {
return fgets($this->_input);
}
+/**
+ * Checks if data is available on the stream
+ *
+ * @param integer $timeout An optional time to wait for data
+ * @return bool True for data available, false otherwise
+ */
+ public function dataAvailable($timeout = 0) {
+ $readFds = array($this->_input);
+ $readyFds = stream_select($readFds, $writeFds, $errorFds, $timeout);
+ return ($readyFds > 0);
+ }
+
}
Something went wrong with that request. Please try again.