| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * NextFlow (http://github.com/nextflow) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link http://github.com/nextflow/nextflow-php for the canonical source repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace NextFlow\Stream\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use NextFlow\Core\Action\AbstractAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * An action that is able to write to a stream. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class StreamWriteAction extends AbstractAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** The output socket. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const SOCKET_OUTPUT = 'out'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** The stream variable socket. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const SOCKET_STREAM = 'stream'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** The data variable socket. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     const SOCKET_DATA = 'data'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Initializes a new instance of this class. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $this->createSocket(self::SOCKET_OUTPUT); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $this->createSocket(self::SOCKET_STREAM); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->createSocket(self::SOCKET_DATA); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Executes the node's logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $streamSocket = $this->getSocket(self::SOCKET_STREAM); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (!$streamSocket || !$streamSocket->hasNodes()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             throw new InvalidArgumentException('No stream to write to.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $dataSocket = $this->getSocket(self::SOCKET_DATA); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (!$dataSocket || !$dataSocket->hasNodes()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw new InvalidArgumentException('There is no data to write.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $streamValue = $streamSocket->getNode(0)->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $dataValue = $dataSocket->getNode(0)->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         fwrite($streamValue, $dataValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->activate(self::SOCKET_OUTPUT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |