break; //Property NameShould be the last One } else { $this->navigation->args[] = $part; } } } MemPool::instance()->set('bong.mvc.controller', $this->navigation->controllerName); MemPool::instance()->set('bong.mvc.method', $this->navigation->methodName); } public function prepareEngine() { switch ($this->navigation->controllerExtension) { case 'json': $this->_engine = EngineFactory::produce('AppJSONServiceEngine'); break; case 'xml': $this->_engine = EngineFactory::produce('AppXMLServiceEngine'); break; case 'prop': $this->_engine = EngineFactory::produce('AppPropertyServiceEngine'); break; case 'res': $this->_engine = EngineFactory::produce('AppResponseServiceEngine'); break; } parent::prepareEngine(); } } RouterFactory::register('AppServiceRouter');
<?php final class StaticContentRouter extends AbstractContentRouter { public function buildNavigation($path) { } } RouterFactory::register('StaticContentRouter');
protected function newRouter($basepath = null) { $factory = new RouterFactory($basepath); return $factory->newInstance(); }
$extParts = explode('.', array_pop($path)); $ext = count($extParts) >= 2 ? end($extParts) : null; $this->navigation->extension = $ext; $preferedExtension = null; switch ($this->navigation->resourceType) { case 'sys': case 'scrap': $preferedExtension = null; break; case 'img': $preferedExtension = 'png'; break; case 'js': $preferedExtension = 'js'; break; case 'css': $preferedExtension = 'css'; break; case 'xslt': $preferedExtension = 'xsl'; break; default: } $this->navigation->preferedExtension = $preferedExtension; $this->navigation->followSpecifiedExtension = $this->navigation->resourceType == 'img' ? !(!$ext && $preferedExtension) : is_null($preferedExtension) || $ext == $preferedExtension; $this->navigation->estimatedContentName = !$this->navigation->followSpecifiedExtension ? rtrim($this->navigation->contentName, '.') . '.' . $preferedExtension : $this->navigation->contentName; //print_r($this->navigation); } } RouterFactory::register('ResourceRouter');
/** * Returns a Suitable ContentRouter Object based upon the URL Pattern * The Caller needs to extract the engine from the Router through the appropiate Getter * the Validate the Engine then Run the Engine * $engine = $router->engine(); * $engine->check(); or $engine->validate(); * $engine->run(); */ public function decide() { $projectName = null; // issue #31 https://github.com/neel/bong/issues/31 $urlparts = strlen(trim($this->url, "/")) ? explode('/', trim($this->url, "/")) : array(); //Slash The Url $reqUrlParts = explode('/', trim($_SERVER['REQUEST_URI'], "/")); $urlroot = '/' . implode('/', array_slice($reqUrlParts, 0, count($reqUrlParts) - count($urlparts))); MemPool::instance()->set("bong.url.root", $urlroot); /** * URL with Default Project: * controllername * controllername/methodname * controllerName.extension/methodName * image.png * The Extension could be anything like dbg, mc, static, sync etc.. * * URL with default / non default Project Name * ~projectName/controllername * ~projectName/controllername/methodname * ~projectName/controllerName.extension/methodName * ~projectName/image.png */ $projectExt = null; if (count($urlparts) && substr_count($urlparts[0], '~') == 1) { $projectName = substr(array_shift($urlparts), 1); if (substr_count($projectName, '.') == 1) { $parts = explode('.', $projectName); $projectName = $parts[0]; $projectExt = $parts[1]; } } elseif (count($urlparts) && substr_count($urlparts[0], '~') > 1) { throw new MalformedUrlException(); } else { $projectName = Fstab::instance()->defaultProjectName(); } if (Fstab::instance()->projectExists($projectName)) { $projectLocation = Fstab::instance()->projectLocation($projectName); if ($projectLocation && is_dir($projectLocation)) { if (is_readable($projectLocation)) { MemPool::instance()->set("bong.project.current", $projectName); Runtime::setCurrentProject($projectName); } else { throw new ProjectDirNotReadableException($projectName . ''); } } else { throw new ProjectDirNotFoundException($projectName . ''); } } else { throw new ProjectNotFoundException($projectName . ''); } $urlbase = '/' . implode('/', array_slice($reqUrlParts, 0, count($reqUrlParts) - count($urlparts))); MemPool::instance()->set("bong.url.base", $urlbase); $urlExtracted = '/' . implode('/', $urlparts); //{ Start FSM Handling /** * Now if invoked /~project.fsm * load the fsm display */ if ($projectExt == 'fsm') { return RouterFactory::produce('FSMRouter'); } //} end FSM Handling $patterns = array('resource.local' => Conf::instance()->evaluate('urlpatterns.resource.local'), 'resource.sys' => Conf::instance()->evaluate('urlpatterns.resource.sys'), 'service.app' => Conf::instance()->evaluate('urlpatterns.service.app'), 'service.spirit' => Conf::instance()->evaluate('urlpatterns.service.spirit')); $type = 'mvc'; //var_dump($urlExtracted); foreach ($patterns as $key => $pattern) { //echo ">> {$urlExtracted} => $pattern\n"; assert('!empty($pattern)' . "/*{$key} => {$pattern}*/"); if (preg_match($pattern, $urlExtracted, $m)) { //echo ">>\tMatched!\n"; $type = $key; break; } } MemPool::instance()->set("bong.router.pattern", $type); /* AbstractContentRouter* */ $router = null; switch ($type) { case 'resource.local': case 'resource.sys': $router = RouterFactory::produce('ResourceRouter'); break; case 'service.app': $router = RouterFactory::produce('AppServiceRouter'); break; case 'service.spirit': $router = RouterFactory::produce('SpiritServiceRouter'); break; case 'mvc': $router = RouterFactory::produce('MVCRouter'); break; } /** * $router = RouterFactory::produce($key); * $router->route(); * -- in Router -- * AbstractContentEngine* $this->engine = $this->engine(); * -- in AbstractContentRouter::engine() -- * return EngineFactory::produce($key); * -- Another Style -- * Factory::Router::produce() * Factory::Engine::produce() */ $router->setProjectName($projectName); $router->buildNavigation($urlparts); $router->prepareEngine(); return $router; }
$this->navigation->args[] = $rack[$i]; } } else { if (count($rack) > 1) { //Ignore if no MethodName is provided $spirit = new stdClass(); $spirit->spiritName = substr($rack[0], 1); //remove the + sign $spirit->methodName = $rack[1]; $spirit->args = array(); for ($i = 2; $i < count($rack); ++$i) { $spirit->args[] = $rack[$i]; } $this->navigation->spirits[] = $spirit; } } } if (is_null($this->navigation->controllerName)) { $this->navigation->controllerName = 'default'; } if (is_null($this->navigation->methodName)) { $this->navigation->methodName = 'main'; } MemPool::instance()->set('bong.mvc.controller', $this->navigation->controllerName); MemPool::instance()->set('bong.mvc.method', $this->navigation->methodName); Runtime::loadModule('mvc'); //print_r($this->navigation); } } RouterFactory::register('MVCRouter');
$this->navigation->spiritName = substr($spiritName, 1); $this->navigation->methodName = $spiritMethodName; $this->navigation->args = $spiritMethodArgs; $this->navigation->spiritExtension = substr($spiritExtension, 1); $this->navigation->spiritInstanceId = $spiritInstanceId; $this->navigation->propertyName = $propertyPos == -1 ? null : substr($propParts[0], 1); Mempool::instance()->set('bong.mvc.controller', $this->navigation->controllerName); MemPool::instance()->set('bong.mvc.method', $this->navigation->controllerMethodName); //print_r($this->navigation); } public function prepareEngine() { switch ($this->navigation->spiritExtension) { case 'json': $this->_engine = EngineFactory::produce('SpiritJSONServiceEngine'); break; case 'xml': $this->_engine = EngineFactory::produce('SpiritXMLServiceEngine'); break; case 'prop': $this->_engine = EngineFactory::produce('SpiritPropertyServiceEngine'); break; case 'res': $this->_engine = EngineFactory::produce('SpiritResponseServiceEngine'); break; } parent::prepareEngine(); } } RouterFactory::register('SpiritServiceRouter');
public function testCreateRoute() { $route = $this->object->createRoute(); $this->assertThat($route, $this->isInstanceOf('Route')); }
<?php class FSMRouter extends AbstractContentRouter { public function __construct() { parent::__construct("FSMEngine"); } public function buildNavigation($path) { } } RouterFactory::register('FSMRouter');
public static function start() { self::$router = RouterFactory::get(); self::$router->start(); }