From b30d5e5fa2de08c3e91c1165ee226fd83f29ba9b Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 12 Oct 2016 11:49:00 -0700 Subject: [PATCH] Moved map method check to Engine. Added get method to Loader. Added new tests. --- .gitignore | 6 ++---- VERSION | 2 +- flight/Engine.php | 15 +++++---------- flight/Flight.php | 4 ---- flight/core/Loader.php | 8 ++++++++ tests/FlightTest.php | 7 +++++++ tests/MapTest.php | 7 +++++++ 7 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 8dfe4d1..a5ec20f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ -composer.phar +.idea vendor/ - -# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file -# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file +composer.phar composer.lock diff --git a/VERSION b/VERSION index 9728bd6..9a83513 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.21 +1.2.22 diff --git a/flight/Engine.php b/flight/Engine.php index 8c0c41a..8989215 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -56,6 +56,7 @@ class Engine { * @param string $name Method name * @param array $params Method parameters * @return mixed Callback results + * @throws \Exception */ public function __call($name, $params) { $callback = $this->dispatcher->get($name); @@ -64,6 +65,10 @@ class Engine { return $this->dispatcher->run($name, $params); } + if (!$this->loader->get($name)) { + throw new \Exception("{$name} must be a mapped method."); + } + $shared = (!empty($params)) ? (bool)$params[0] : true; return $this->loader->load($name, $shared); @@ -272,16 +277,6 @@ class Engine { $this->loader->addDirectory($dir); } - /** - * Checks for mapped event within the engine dispatcher. - * - * @param $name - * @return bool - */ - public function isEvent($name) { - return !is_null($this->dispatcher->get($name)); - } - /*** Extensible Methods ***/ /** diff --git a/flight/Flight.php b/flight/Flight.php index c2de4f9..296457a 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -74,10 +74,6 @@ class Flight { public static function __callStatic($name, $params) { $app = Flight::app(); - if (!method_exists($app, $name) && !$app->isEvent($name)) { - throw new \Exception("{$name} must be a mapped method"); - } - return \flight\core\Dispatcher::invokeMethod(array($app, $name), $params); } diff --git a/flight/core/Loader.php b/flight/core/Loader.php index f216f06..289d3b0 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -137,6 +137,14 @@ class Loader { } } + /** + * @param string $name Registry name + * @return mixed Class information or null if not registered + */ + public function get($name) { + return isset($this->classes[$name]) ? $this->classes[$name] : null; + } + /** * Resets the object to the initial state. */ diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 0c286a9..2a778c5 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -73,4 +73,11 @@ class FlightTest extends PHPUnit_Framework_TestCase $this->assertEquals('hello', $result); } + + // Unmapped method + function testUnmapped() { + $this->setExpectedException('Exception', 'doesNotExist must be a mapped method.'); + + Flight::doesNotExist(); + } } diff --git a/tests/MapTest.php b/tests/MapTest.php index 12b0828..8a6ea8b 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -62,4 +62,11 @@ class MapTest extends PHPUnit_Framework_TestCase $this->assertEquals('goodbye', $result); } + + // Unmapped method + function testUnmapped() { + $this->setExpectedException('Exception', 'doesNotExist must be a mapped method.'); + + $this->app->doesNotExist(); + } }