Search code examples
phparrayslaravelclosureslaravelcollective

Return correct array from closure


I use on my code a closure with SSH of LaravelCollective on Laravel 5.3

But my surpise it's when try return exit for any commands, get a truncate result.

$exit = array();
SSH::run($cmd, function ($line) use (&$exit) {
    echo $line.PHP_EOL;
    $exit[] = $line; 
});
dd($exit) // dd it's a var_dump + exit helper on Laravel

Well this code return a first part (echo $line.PHP_EOL) correct such below

/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.appdata
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.autorespond
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.cpanel
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.cphorde
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.cpremote
/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.elinks
... 

And wrong result on array. 4

array:5 [
  0 => """
    /Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home\n
    /Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0\n
    /Volumes/MACBAC
    ...
    """
  4 => """
    UP/cprsync_remote/hq/daily/users/tamainut/home/daily.8/tmp\n
  /Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.8/updates\n
    /Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.9\n
    /Volumes/MACBACKUP/cprsync_r

I try several options but any work fine.

I don't understand why i see perfect echo lines, but can't insert on array for work more later.

EDIT after some comments.

I see that problem it's closure, get a lineof SSH command how string with "\n". After get max size for string truncate, and run a new iteration of closure. That it's problem.


Solution

  • After some tests I have found a solution that although it does not seem very appropriate works.

    The problem is that the SSH run method, returns as many string as necessary, for a byte limit, which I do not know.

    We can not use an array, since the array would contain truncated elements, so the best thing in my opinion is to extract the line as a string and treat it later.

    If someone can reply with a most grateful response,

    $exit = '';
    SSH::run($cmd, function ($line) use (&$exit) {
       $exit = $exit.$line;
    });
    $arr = explode("\n",$exit);
    dd($arr);
    

    Show correct result

    array:863 [
      0 => "/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home"
      1 => "/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0"
      2 => "/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.0/.appdata"
    ...
    
     861 => "/Volumes/MACBACKUP/cprsync_remote/hq/daily/users/tamainut/home/daily.9/updates"
      862 => ""
    ]
    

    Attention to limit of string (2GB but other limitations imposes by memory_limit)