Beispiel #1
0
    public function run($configs)
    {/*{{{*/
        $result = array();
        $result['errno'] = -1;
        $result['data'] = array();
        try
        {
            BeanFinder::register('configs', new Configuration($configs));
            $this->initDebug();
            Logger::setUp($configs['system']);
            BeanFinder::get('debug')->begin('service', $this->service.'::'.$this->do, true);

            //是否需要进程间同步
            $synchronized = isset($this->options['synchronized'])?$this->options['synchronized']:false;
            if ($synchronized)
            {
                $cacher= DAL::get()->getCache(Cacher::CACHETYPE_LOCKER);
                $lockUtil  = LockUtil::factory(LockUtil::LOCK_TYPE_MEMCACHE, array('memcache' => $cacher));
                $lockName = $this->service.$this->do;

                //client来决定同步标志,同一个方法,可以根据不用的标志来实施不同的锁
                //同一个service,synchronizedKey 不能重复
                $synchronizedKey = isset($this->options['synchronizedKey'])?$this->options['synchronizedKey']:false;
                if ($synchronizedKey)
                {
                    $lockName = $this->service.':'.$synchronizedKey;
                }
                $lockName = strtolower($lockName);
                $lockUtil->getLock($lockName, 10);
            }

            $uw = new UnitOfWork();
            $service = new $this->service;
            $service->initialize($this->options);
            $this->checkMethod($service);
            

            //error_log("files: ".print_r($_FILES, true), 3, '/tmp/a');
            //error_log("args: ".print_r($this->args, true), 3, '/tmp/a');
            //error_log("service: ".print_r($service, true), 3, '/tmp/a');
            //error_log("do: ".print_r($this->do, true), 3, '/tmp/a');
            //error_log("options: ".print_r($this->options, true), 3, '/tmp/a');
            $result['data'] = call_user_func_array(array($service, $this->do), $this->args);

            $uw->commit();

            BeanFinder::get('debug')->end();
            $result = $this->terminateDebug($result);

            $service->terminate();

            if ($synchronized && $lockUtil)
			    $lockUtil->releaseLock($lockName);

            $uw = $service = null;
            $this->checkOutput($result['data']);
            $result['errno'] = 0;
        }
        catch (LockException $e)
        {
            $result['errmsg']  = 'LockTimeOut';
            $result['exception'] = get_class($e);
            $result['ex'] = $e;
            //do some log here
            error_log(date('Y-m-d H:i:s')." ".print_r(unserialize(serialize($e)), true), 3, '/tmp/exception_sys.log');

            BeanFinder::get('debug')->addParams($e)->autoCloseNode();
            $result = $this->terminateDebug($result);
        }
        catch (BizException $e)
        {
            $result['errmsg']  = $e->getMessage();
            $result['exception'] = get_class($e);
            $result['ex'] = $e;
            //do some log here
            //error_log(print_r($e, true), 3, '/tmp/exception_biz.log');

            if ($synchronized && $lockUtil)
			    $lockUtil->releaseLock($lockName);

            BeanFinder::get('debug')->addParams($e)->autoCloseNode();
            $result = $this->terminateDebug($result);
        }
        catch (SystemException $e)
        {
            $result['errmsg']  = '系统错误';
            $result['exception'] = get_class($e);
            $result['ex'] = $e;
            //do some log here
            error_log(date('Y-m-d H:i:s')." ".print_r(unserialize(serialize($e)), true), 3, '/tmp/exception_sys.log');

            if ($synchronized && $lockUtil)
			    $lockUtil->releaseLock($lockName);

            BeanFinder::get('debug')->addParams($e)->autoCloseNode();
            $result = $this->terminateDebug($result);
        }
        catch (CustomException $e)
        {
            $result['errmsg']  = '系统错误';
            $result['exception'] = get_class($e);
            $result['ex'] = $e;
            //do some log here
            error_log(date('Y-m-d H:i:s')." ".print_r($e->errorMessage(), true), 3, '/tmp/exception_sys.log');

            if ($synchronized && $lockUtil)
			    $lockUtil->releaseLock($lockName);

            BeanFinder::get('debug')->addParams($e)->autoCloseNode();
            $result = $this->terminateDebug($result);
        }
        catch (Exception $e)
        {
            $result['errmsg']  = '系统错误';
            $result['exception'] = get_class($e);
            $result['ex'] = $e;
            //do some log here
            error_log(date('Y-m-d H:i:s')." ".print_r(unserialize(serialize($e)), true), 3, '/tmp/exception_sys.log');

            if ($synchronized && $lockUtil)
			    $lockUtil->releaseLock($lockName);

            BeanFinder::get('debug')->addParams($e)->autoCloseNode();
            $result = $this->terminateDebug($result);
        }
        return $this->prepareOutput($result);
    }/*}}}*/
Beispiel #2
0
// Require all files
require 'MapperRegistry.php';
require 'DomainObject.php';
require 'CustomerMapper.php';
require 'Customer.php';
require 'UnitOfWork.php';
// Initiate the mapper registry, with DAO so that injection into the mappers becomes easy.
$mapperRegistry = new MapperRegistry();
$mapperRegistry->setDao(new DAO());
// Initiating the unit of work object
$unitOfWork = new UnitOfWork($mapperRegistry);
// Fire up our mapper, this will take care of the fetching and storing logic
$mapper = $mapperRegistry->getMapperForClassName('Customer');
// Retrieve a customer, with ID 42.
$customer = $mapper->findById(42);
// -- At this point, the customer is marked "clean" in the Unit of Work object.
// Change the e-mail to something different
//
// Once PHP get's field getters and setters, this can be done by using public fields instead of getters and setters.
// @see https://wiki.php.net/rfc/propertygetsetsyntax
$customer->email = '*****@*****.**';
// We changed the customer, let's mark it as dirty.
// Because we are using "called registration", we have to manually do all the thinking for registering objects. This
// is explicit and is fine when the code allows for a natural flow. However if the code is more complex. This
// approach might be too error prone and the "object registration" approach is a much safer choice.
$unitOfWork->registerDirty($customer);
// -- At this point, the customer is marked "dirty" in the Unit of Work object.
// All done, store the customer back to the database. Note that we trigger storing of our entities using the unit of
// work object.
$unitOfWork->commit();