/**
  * @return \Phalcon\DiInterface
  */
 public function createDI()
 {
     $di = new \Phalcon\Di();
     $diClassInstance = new $this->diClass();
     $reflectionClass = new \ReflectionClass($this->diClass);
     $methods = $reflectionClass->getMethods();
     foreach ($methods as $method) {
         $di->setRaw($method->name, call_user_func([$diClassInstance, $method->name]));
     }
     return $di;
 }
 /**
  * @return \Phalcon\DiInterface
  *
  * @throws \Exception
  */
 protected function createDI()
 {
     $di = new \Phalcon\Di();
     $reflectionClass = new \ReflectionClass($this->diClass);
     $methods = $reflectionClass->getMethods(\ReflectionMethod::IS_STATIC);
     $annotations = $this->getAnnotations();
     foreach ($methods as $method) {
         $annotationsMethod = $annotations->getMethod($this->diClass, $method->name);
         if (!$annotationsMethod->has('Shared')) {
             throw new \Exception('A DI service does not have a @Shared annotation: ' . $this->diClass . '::' . $method->name);
         }
         $isShared = $annotationsMethod->get('Shared')->getArgument(0);
         $di->set($method->name, $method->getClosure(), $isShared);
     }
     return $di;
 }
Beispiel #3
0
 public function testIssue1242()
 {
     $di = new \Phalcon\Di();
     $di->set('resolved', function () {
         return new Service1242();
     });
     $di->set('notresolved', function () {
         return new Service1242();
     });
     $this->assertFalse($di->getService('resolved')->isResolved());
     $this->assertFalse($di->getService('notresolved')->isResolved());
     $di->get('resolved');
     $this->assertTrue($di->getService('resolved')->isResolved());
     $this->assertFalse($di->getService('notresolved')->isResolved());
 }
Beispiel #4
0
 /**
  * "Attaching event listeners by event name fails if preceded by
  * detachment of all listeners for that type."
  *
  * Test contains 4 steps:
  * - assigning event manager to dummy service with single log event
  *   listener attached
  * - attaching second log listener
  * - detaching all log listeners
  * - attaching different listener
  *
  * NOTE: This test looks the same as above but it checks detachAll()
  * instead of detachAll() method. To be DELETED when detachAll()
  * will not supported any more.
  *
  * @see https://github.com/phalcon/cphalcon/issues/1331
  */
 public function testBug1331BackwardCompatibility()
 {
     $di = new Phalcon\Di();
     $di->set('componentX', function () use($di) {
         $component = new LeDummyComponent();
         $eventsManager = new Phalcon\Events\Manager();
         $eventsManager->attach('log', $di->get('MyFirstWeakrefListener'));
         $component->setEventsManager($eventsManager);
         return $component;
     });
     $di->set('firstListener', 'MyFirstWeakrefListener');
     $di->set('secondListener', 'MySecondWeakrefListener');
     // ----- TESTING STEP 1 - SIGNLE 'LOG' LISTENER ATTACHED
     $component = $di->get('componentX');
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertCount(1, $logListeners);
     $this->assertInstanceOf('MyFirstWeakrefListener', $logListeners[0]);
     // ----- TESTING STEP 2 - SECOND 'LOG' LISTENER ATTACHED
     $component->getEventsManager()->attach('log', $di->get('MySecondWeakrefListener'));
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertCount(2, $logListeners);
     $firstLister = array_shift($logListeners);
     $secondLister = array_shift($logListeners);
     $this->assertInstanceOf('MyFirstWeakrefListener', $firstLister);
     $this->assertInstanceOf('MySecondWeakrefListener', $secondLister);
     // ----- TESTING STEP 3 - ALL 'LOG' LISTENER DETACHED
     $component->getEventsManager()->detachAll('log');
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertEmpty($logListeners);
     // ----- TESTING STEP 4 - SINGLE 'LOG' LISTENER ATTACHED SECOND TIME
     $component->getEventsManager()->attach('log', $di->get('MySecondWeakrefListener'));
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertCount(1, $logListeners);
     $this->assertInstanceOf('MySecondWeakrefListener', $logListeners[0]);
 }
 public function testDispatcher()
 {
     Phalcon\DI::reset();
     $di = new Phalcon\Di();
     $di->set('response', new \Phalcon\Http\Response());
     $dispatcher = new Phalcon\Mvc\Dispatcher();
     $dispatcher->setDI($di);
     $this->assertInstanceOf('Phalcon\\Di', $dispatcher->getDI());
     $di->set('dispatcher', $dispatcher);
     $dispatcher->setControllerName('index');
     $dispatcher->setActionName('index');
     $dispatcher->setParams(array());
     try {
         $dispatcher->dispatch();
         $this->assertTrue(FALSE, 'oh, Why?');
     } catch (Phalcon\Exception $e) {
         $this->assertEquals($e->getMessage(), "IndexController handler class cannot be loaded");
         $this->assertInstanceOf('Phalcon\\Mvc\\Dispatcher\\Exception', $e);
     }
     $dispatcher->setControllerName('essai');
     $dispatcher->setActionName('index');
     $dispatcher->setParams(array());
     try {
         $dispatcher->dispatch();
         $this->assertTrue(FALSE, 'oh, Why?');
     } catch (Phalcon\Exception $e) {
         $this->assertEquals($e->getMessage(), "EssaiController handler class cannot be loaded");
         $this->assertInstanceOf('Phalcon\\Mvc\\Dispatcher\\Exception', $e);
     }
     $dispatcher->setControllerName('test0');
     $dispatcher->setActionName('index');
     $dispatcher->setParams(array());
     try {
         $dispatcher->dispatch();
         $this->assertTrue(FALSE, 'oh, Why?');
     } catch (Phalcon\Exception $e) {
         $this->assertEquals($e->getMessage(), "Test0Controller handler class cannot be loaded");
         $this->assertInstanceOf('Phalcon\\Mvc\\Dispatcher\\Exception', $e);
     }
     $dispatcher->setControllerName('test1');
     $dispatcher->setActionName('index');
     $dispatcher->setParams(array());
     try {
         $dispatcher->dispatch();
         $this->assertTrue(FALSE, 'oh, Why?');
     } catch (Phalcon\Exception $e) {
         $this->assertEquals($e->getMessage(), "Action 'index' was not found on handler 'test1'");
     }
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('index');
     $dispatcher->setParams(array());
     $controller = $dispatcher->dispatch();
     $this->assertInstanceOf('Test2Controller', $controller);
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('essai');
     $dispatcher->setParams(array());
     try {
         $dispatcher->dispatch();
         $this->assertTrue(FALSE, 'oh, Why?');
     } catch (Phalcon\Exception $e) {
         $this->assertEquals($e->getMessage(), "Action 'essai' was not found on handler 'test2'");
     }
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('other');
     $dispatcher->setParams(array());
     $controller = $dispatcher->dispatch();
     $this->assertInstanceOf('Test2Controller', $controller);
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('another');
     $dispatcher->setParams(array());
     $dispatcher->dispatch();
     $value = $dispatcher->getReturnedValue();
     $this->assertEquals($value, 100);
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('anotherTwo');
     $dispatcher->setParams(array(2, "3"));
     $dispatcher->dispatch();
     $value = $dispatcher->getReturnedValue();
     $this->assertEquals($value, 5);
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('anotherthree');
     $dispatcher->setParams(array());
     $dispatcher->dispatch();
     $value = $dispatcher->getActionName();
     $this->assertEquals($value, 'anotherfour');
     $value = $dispatcher->getReturnedValue();
     $this->assertEquals($value, 120);
     $dispatcher->setControllerName('test2');
     $dispatcher->setActionName('anotherFive');
     $dispatcher->setParams(array("param1" => 2, "param2" => 3));
     $dispatcher->dispatch();
     $value = $dispatcher->getReturnedValue();
     $this->assertEquals($value, 5);
     $dispatcher->setControllerName('test7');
     $dispatcher->setActionName('service');
     $dispatcher->setParams(array());
     $dispatcher->dispatch();
     $value = $dispatcher->getReturnedValue();
     $this->assertEquals($value, "hello");
     $value = $dispatcher->getControllerClass();
     $this->assertEquals($value, "Test7Controller");
 }
require_once 'CommonDispatcher.php';
use App\Hooks\PreControllerBase;
use Phalcon\Di\FactoryDefault;
//use Phalcon\Mvc\View;
use Phalcon\Events\Event;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
//use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Mvc\View;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Logger\Adapter\File as FileLogger;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Mvc\Dispatcher as MvcDispatcher;
//$di = new FactoryDefault();
$di = Phalcon\Di::getDefault();
$config = $di->getConfig();
/**
 * set up file logger
 */
$oLogger = new FileLogger($config->application->logDir . 'app.log');
$di->setShared('fileLogger', $oLogger);
//$dispatcherEventsManager = new EventsManager();
//
//$di->setShared('dispatcher', function() use($dispatcherEventsManager){
//
//	$dispatcher = new CommonDispatcher();
//	$dispatcher->setEventsManager($dispatcherEventsManager);
//	return $dispatcher;
//});
//