Example #1
0
 /**
  * @param Manager $manager
  * @param Request $request
  * @return Response
  * @throws Exception
  */
 function handle(Manager $manager, Request $request)
 {
     $response = $request->generateResponse();
     if (isset($this->responses[$request->getTid()])) {
         return $this->responses[$request->getTid()];
     }
     try {
         if (!isset($this->classes[$request->getAction()])) {
             throw new Exception(sprintf("Action %s not found", $request->getAction()));
         }
         foreach ($this->actions[$request->getAction()] as $info) {
             if ($request->getMethod() == $info['name']) {
                 $response->setType($request->getType());
                 $response->setResult($manager->call($this->classes[$request->getAction()], $request->getMethod(), $request->getData()));
                 break;
             }
         }
         if (!$response->getType()) {
             throw new Exception(sprintf("Method %s not found", $response->getMethod()));
         }
     } catch (Exception $e) {
         $response->setType('exception');
         $response->setResult($e->getMessage());
         $response->setFile($e->getFile());
         $response->setLine($e->getLine());
     }
     return $this->responses[$response->getTid()] = $response;
 }
Example #2
0
File: WebTest.php Project: cti/core
 public function testUrl()
 {
     $manager = new Manager();
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $_SERVER['REQUEST_URI'] = '/application-url/direct';
     $web = $manager->create('Cti\\Core\\Module\\Web', array('base' => '/application-url/'));
     $this->assertSame($web->getUrl('test'), '/application-url/test');
 }
Example #3
0
 public function testMethod()
 {
     $manager = new Manager();
     $manager->getInitializer()->before('Common\\Module', array($this, 'callBefore'));
     $manager->getInitializer()->after('Common\\Module', array(__CLASS__, 'callAfter'));
     $module = $manager->create('Common\\Module');
     $this->assertSame($module->state, 'Cti\\Di\\Manager');
     $this->assertSame($module->reference, $manager);
 }
Example #4
0
File: UsageTest.php Project: cti/di
 function testUsing()
 {
     $manager = new Manager();
     /**
      * @var \Common\Usage $usage
      */
     $usage = $manager->create('Common\\Usage');
     $this->assertSame($usage->manager, $manager);
     $this->assertSame($usage->config, $manager->getConfiguration());
     $this->assertSame($usage->globalManager, $manager);
 }
Example #5
0
 function getJobList(JobRepository $repository, Manager $manager)
 {
     $rows = array();
     foreach ($repository->find(array(), 'many') as $item) {
         $rows[] = $item->asArray();
     }
     if (!count($rows)) {
         $initializer = $manager->create('Scheduler\\Init');
         return $this->getJobList($repository, $manager);
     }
     return array('data' => $rows);
 }
Example #6
0
File: NewTests.php Project: cti/di
 function testNew()
 {
     $manager = new Manager();
     $new = $manager->getInstance('Common\\InjectNew');
     $this->assertInstanceOf('Common\\InjectNew', $new);
     $this->assertInstanceOf('Common\\Module', $new->module1);
     $this->assertInstanceOf('Common\\Module', $new->module2);
     $this->assertNotSame($new->module1, $new->module2);
     $this->assertSame($manager->getInjector()->getReferences($new->module1), array(array('instance' => $new, 'property' => 'module1')));
     $this->assertSame($manager->getInstances('Common\\Module'), array($new->module1, $new->module2));
     $new2 = $manager->create('Common\\InjectNew');
     $this->assertInstanceOf('Common\\InjectNew', $new2);
     $this->assertInstanceOf('Common\\Module', $new2->module1);
     $this->assertInstanceOf('Common\\Module', $new2->module2);
     $this->assertNotSame($new2->module1, $new2->module2);
     $this->assertNotSame($new->module1, $new2->module1);
     $this->assertNotSame($new->module2, $new2->module2);
     $this->assertSame($manager->getInstances('Common\\Module'), array($new->module1, $new->module2, $new2->module1, $new2->module2));
     $references = array($new, $new, $new2, $new2);
     foreach ($manager->getInstances('Common\\Module') as $module) {
         foreach ($manager->getInjector()->getReferences($module) as $reference) {
             $actual[] = $reference['instance'];
         }
     }
     $this->assertSame($references, $actual);
 }
Example #7
0
 function testCalling()
 {
     $manager = new Manager();
     $tid = 1;
     $manager->getConfiguration()->set('Cti\\Direct\\Service', 'list', array('Common\\Api'));
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'greet', 'tid' => $tid, 'type' => 'type', 'data' => array('Cti')))));
     $this->assertSame($response->getResult(), "Hello, Cti!");
     // test cache by tid
     $responseWithSameTid = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'anotherGreet', 'tid' => $tid, 'type' => 'type', 'data' => array('Cti2')))));
     $this->assertSame($response, $responseWithSameTid);
     // test exception
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'exception', 'tid' => ++$tid, 'type' => 'type', 'data' => array('message')))));
     $this->assertSame($response->getType(), 'exception');
     $this->assertSame($response->getResult(), 'message');
     $this->assertNotNull($response->getFile());
     $this->assertNotNull($response->getLine());
     // test incorrect action
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'no_action', 'method' => 'no_method', 'tid' => ++$tid, 'type' => 'type', 'data' => array()))));
     $this->assertSame($response->getType(), 'exception');
     $this->assertContains("Action no_action not found", $response->getResult());
     // test incorrect method
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'no_method', 'tid' => ++$tid, 'type' => 'type', 'data' => array()))));
     $this->assertSame($response->getType(), 'exception');
     $this->assertContains("Method no_method not found", $response->getResult());
 }
Example #8
0
File: CacheTest.php Project: cti/di
 public function testCache()
 {
     $manager = new Manager();
     $startTime = microtime(1);
     $manager->get('Inject\\FullClassName');
     $manager->get('Inject\\ShortClassName');
     $manager->get('Inject\\ByNamespace');
     $manager->call('Common\\Application', 'extractModuleFromManager');
     $parsingTime = microtime(1) - $startTime;
     $data = $manager->get('Cti\\Di\\Cache')->getData();
     $manager = new Manager();
     $manager->get('Cti\\Di\\Cache')->setData($data);
     $startTime = microtime(1);
     $manager->get('Inject\\FullClassName');
     $manager->get('Inject\\ShortClassName');
     $manager->get('Inject\\ByNamespace');
     $manager->call('Common\\Application', 'extractModuleFromManager');
     $cachedTime = microtime(1) - $startTime;
     $this->assertGreaterThan($cachedTime, $parsingTime);
     $this->assertNull($manager->get('Cti\\Di\\Cache')->get(2, array(3)));
 }
Example #9
0
 public function registerCommands(Console $console, Manager $manager)
 {
     foreach ($this->getClasses('Command') as $class) {
         $console->add($manager->get($class));
     }
 }
Example #10
0
File: DiTests.php Project: cti/di
 public function testEmptyClass()
 {
     $this->setExpectedException('Exception');
     $m = new Manager();
     $m->get('');
 }
Example #11
0
 public function extractModuleFromManager(Manager $manager)
 {
     return $manager->get('Common\\Module');
 }
Example #12
0
 function testInstantiate()
 {
     $manager = new Manager();
     $this->assertInstanceOf('Cti\\Di\\Locator', $manager->getLocator());
 }
Example #13
0
 /**
  * Create new model
  * @param $name
  * @param $comment
  * @param array $properties
  * @return Model
  */
 public function createModel($name, $comment, $properties = array(), $pk = array())
 {
     $this->models[$name] = $this->manager->create('Cti\\Storage\\Component\\Model', array('name' => $name, 'comment' => $comment, 'properties' => $properties, 'pk' => $pk));
     $this->models[$name]->setNamespace($this->getNamespace());
     return $this->models[$name];
 }