Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Swoole\Component; use Swoole\Exception; use Swoole\IFace; class Event { ..

Decoded Output download

<?php
namespace Swoole\Component;

use Swoole\Exception;
use Swoole\IFace;

class Event
{
    /**
     * @var IFace\Queue
     */
    protected $_queue;
    protected $_handles = array();

    /**
     * @var \swoole_atomic
     */
    protected $_atomic;
    protected $_workers = array();

    protected $config;
    protected $async = false;

    function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * 
     * @param $type
     * @param $data
     * @return mixed
     */
    protected function _execute($type, $data)
    {
        if (!isset($this->_handles[$type]))
        {
            $handlers = array();
            $path = \Swoole::$app_path.'/events/'.$type.'.php';
            if (is_file($path))
            {
                $_conf = include $path;
                if ($_conf and isset($_conf['handlers']) and is_array($_conf['handlers']))
                {
                    foreach ($_conf['handlers'] as $h)
                    {
                        if (class_exists($h))
                        {
                            $object = new $h;
                            if ($object instanceof IFace\EventHandler)
                            {
                                $handlers[] = $object;
                                continue;
                            }
                        }
                        trigger_error("invalid event handler[$type|$h]", E_USER_WARNING);
                    }
                }
            }
            $this->_handles[$type] = $handlers;
        }

        foreach($this->_handles[$type] as $handler)
        {
            /**
             * @var  $handler  IFace\EventHandler
             */
            $handler->trigger($type, $data);
        }
        return true;
    }

    /**
     * 
     * @param $type
     * @param $data
     * @return mixed
     * @throws Exception\NotFound
     */
    function trigger($type, $data)
    {
        /**
         * 
         */
        if (isset($this->config['async']) && $this->config['async'])
        {
            return $this->getQueueInstance()->push(array('type' => $type, 'data' => $data));
        }
        /**
         * 
         */
        else
        {
            return $this->_execute($type, $data);
        }
    }

    function _worker()
    {
        $queue = $this->getQueueInstance();

        while ($this->_atomic->get() == 1)
        {
            $event = $queue->pop();
            if ($event)
            {
                $this->_execute($event['type'], $event['data']);
            }
            else
            {
                usleep(100000);
            }
        }
    }

    /**
     * @param int $worker_num
     * @param bool $daemon
     * @throws Exception\NotFound
     */
	function runWorker($worker_num = 1, $daemon = false)
    {
        if ($worker_num > 1 or $daemon)
        {
            if (!class_exists('\swoole\process'))
            {
                throw new Exception\NotFound("require swoole extension");
            }
            if ($worker_num < 0 or $worker_num > 1000)
            {
                $worker_num = 200;
            }
        }
        else
        {
            $this->_atomic = new \swoole_atomic(1);
            $this->_worker();
            return;
        }

        if ($daemon)
        {
            \swoole_process::daemon();
        }

        $this->_atomic = new \swoole_atomic(1);
        for ($i = 0; $i < $worker_num; $i++)
        {
            $process = new \swoole\process(array($this, '_worker'), false, false);
            $process->start();
            $this->_workers[] = $process;
        }

        \swoole_process::signal(SIGCHLD, function() {
            while(true) {
                $exitProcess = \swoole_process::wait(false);
                if ($exitProcess)
                {
                    foreach ($this->_workers as $k => $p)
                    {
                        if ($p->pid == $exitProcess['pid'])
                        {
                            if ($this->_atomic->get() == 1)
                            {
                                $p->start();
                            }
                            else
                            {
                                unset($this->_workers[$k]);
                                if (count($this->_workers) == 0)
                                {
                                    swoole_event_exit();
                                }
                            }
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        });

        \swoole_process::signal(SIGTERM, function() {
            //
            $this->_atomic->set(0);
        });
    }

    protected function getQueueInstance()
    {
        $class = $this->config['type'];
        if (!class_exists($class))
        {
            throw new Exception\NotFound("class $class not found.");
        }

        if (is_null($this->_queue))
        {
            $this->_queue = new $class($this->config);
        }

        return $this->_queue;
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php
namespace Swoole\Component;

use Swoole\Exception;
use Swoole\IFace;

class Event
{
    /**
     * @var IFace\Queue
     */
    protected $_queue;
    protected $_handles = array();

    /**
     * @var \swoole_atomic
     */
    protected $_atomic;
    protected $_workers = array();

    protected $config;
    protected $async = false;

    function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * 
     * @param $type
     * @param $data
     * @return mixed
     */
    protected function _execute($type, $data)
    {
        if (!isset($this->_handles[$type]))
        {
            $handlers = array();
            $path = \Swoole::$app_path.'/events/'.$type.'.php';
            if (is_file($path))
            {
                $_conf = include $path;
                if ($_conf and isset($_conf['handlers']) and is_array($_conf['handlers']))
                {
                    foreach ($_conf['handlers'] as $h)
                    {
                        if (class_exists($h))
                        {
                            $object = new $h;
                            if ($object instanceof IFace\EventHandler)
                            {
                                $handlers[] = $object;
                                continue;
                            }
                        }
                        trigger_error("invalid event handler[$type|$h]", E_USER_WARNING);
                    }
                }
            }
            $this->_handles[$type] = $handlers;
        }

        foreach($this->_handles[$type] as $handler)
        {
            /**
             * @var  $handler  IFace\EventHandler
             */
            $handler->trigger($type, $data);
        }
        return true;
    }

    /**
     * 
     * @param $type
     * @param $data
     * @return mixed
     * @throws Exception\NotFound
     */
    function trigger($type, $data)
    {
        /**
         * 
         */
        if (isset($this->config['async']) && $this->config['async'])
        {
            return $this->getQueueInstance()->push(array('type' => $type, 'data' => $data));
        }
        /**
         * 
         */
        else
        {
            return $this->_execute($type, $data);
        }
    }

    function _worker()
    {
        $queue = $this->getQueueInstance();

        while ($this->_atomic->get() == 1)
        {
            $event = $queue->pop();
            if ($event)
            {
                $this->_execute($event['type'], $event['data']);
            }
            else
            {
                usleep(100000);
            }
        }
    }

    /**
     * @param int $worker_num
     * @param bool $daemon
     * @throws Exception\NotFound
     */
	function runWorker($worker_num = 1, $daemon = false)
    {
        if ($worker_num > 1 or $daemon)
        {
            if (!class_exists('\swoole\process'))
            {
                throw new Exception\NotFound("require swoole extension");
            }
            if ($worker_num < 0 or $worker_num > 1000)
            {
                $worker_num = 200;
            }
        }
        else
        {
            $this->_atomic = new \swoole_atomic(1);
            $this->_worker();
            return;
        }

        if ($daemon)
        {
            \swoole_process::daemon();
        }

        $this->_atomic = new \swoole_atomic(1);
        for ($i = 0; $i < $worker_num; $i++)
        {
            $process = new \swoole\process(array($this, '_worker'), false, false);
            $process->start();
            $this->_workers[] = $process;
        }

        \swoole_process::signal(SIGCHLD, function() {
            while(true) {
                $exitProcess = \swoole_process::wait(false);
                if ($exitProcess)
                {
                    foreach ($this->_workers as $k => $p)
                    {
                        if ($p->pid == $exitProcess['pid'])
                        {
                            if ($this->_atomic->get() == 1)
                            {
                                $p->start();
                            }
                            else
                            {
                                unset($this->_workers[$k]);
                                if (count($this->_workers) == 0)
                                {
                                    swoole_event_exit();
                                }
                            }
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        });

        \swoole_process::signal(SIGTERM, function() {
            //
            $this->_atomic->set(0);
        });
    }

    protected function getQueueInstance()
    {
        $class = $this->config['type'];
        if (!class_exists($class))
        {
            throw new Exception\NotFound("class $class not found.");
        }

        if (is_null($this->_queue))
        {
            $this->_queue = new $class($this->config);
        }

        return $this->_queue;
    }
}

Function Calls

None

Variables

None

Stats

MD5 2d72e5263b573b8082073b3cd2a0dc94
Eval Count 0
Decode Time 118 ms