public function testObject() { $this->assertEquals('foo', Text::left('foobar', 3)); $this->assertEquals('foo', Text::left('foobar', 'foo')); $this->assertEquals('bar', Text::mid('foobarqux', 3, 3)); $this->assertEquals('bar', Text::mid('foobarqux', 'foo', 'bar')); $this->assertEquals('bar', Text::right('foobar', 3)); $this->assertEquals('bar', Text::right('foobar', 'bar')); $this->assertEquals('bar', Text::offsetLeft('foobar', 3)); $this->assertEquals('bar', Text::offsetLeft('foobar', 'foo')); $this->assertEquals('foo', Text::offsetRight('foobar', 3)); $this->assertEquals('foo', Text::offsetRight('foobar', 'bar')); $this->assertEquals(3, Text::len('foo')); $this->assertEquals(4, Text::len('über')); $this->assertNotEquals(4, Text::len('über', null)); $this->assertEquals('foo-bar-qux', Text::splitCamelCase('FooBarQux')); $this->assertEquals('foo-bar-qux', Text::splitCamelCase('fooBarQux')); $this->assertEquals('fooBarQux', Text::toCamelCase('foo-bar-qux')); $this->assertEquals('FooBarQux', Text::toCamelCase('foo-bar-qux', true)); $this->assertEquals('foo="bar" qux="baz" asd="kos"', Text::createAttributeString(array('foo' => 'bar', 'qux' => 'baz', 'asd' => 'kos'))); for ($i = 5; $i <= 10; $i++) { $string = str_repeat('x', $i); $this->assertEquals($string, Text::shorten($string, 10)); } for ($i = 11; $i <= 15; $i++) { $string = str_repeat('x', $i); $this->assertEquals(10, Text::len(Text::shorten($string, 10))); } }
/** * Register a route with the application * * @param $name * @param string $pattern the slim compatible url pattern * @param array $params params that will be passed on to the controller * @param array $methods methods this controller accepts * * @throws Exception_UnknownControllerClass * @throws \InvalidArgumentException * @throws Exception * @return \Slim_Route */ public function registerRoute($name, $pattern, array $params = array(), array $methods = array(\Slim_Http_Request::METHOD_GET, \Slim_Http_Request::METHOD_POST)) { // ensure argument validity if (!is_string($name)) { throw new \InvalidArgumentException("Argument 'name' needs to be a string"); } if (!is_string($pattern)) { throw new \InvalidArgumentException("Argument 'pattern' needs to be a string"); } // create dispatching function $this->params = isset($this->params) ? $this->params : array(); $request = $this->slim()->request(); $dispatch = function () use($params, $request) { $matched = $this->slim()->router()->getMatchedRoutes($request->getMethod(), $request->getResourceUri()); // get the correct matched route. go back from the end of the array by n passes $offset = $this->dispatchedRoute(false); $current = $matched[$offset]; // merge parameters extracted from uri with hard parameters, passed to registerRoute $params = array_merge($params, $current->getParams()); if (!isset($params['controller'])) { throw new Exception(sprintf("Route with pattern '%s' has no controller parameter", $current->getPattern())); } // assemble action $action = isset($params['action']) ? $params['action'] : 'dispatch'; // - force lowercase action names if ($action != strtolower($action)) { throw new Exception('Action parameter needs to be lowercase. Camel cased action names need to be hyphenated (fooBar -> foo-bar)'); } $action = Text::toCamelCase($action) . 'Action'; // equalize controller names // foo:bar -> default\namespace\foo:bar $class = $params['controller']; $namespace = $this->controllerNamespace; if (isset($params[self::CONTROLLER_NAMESPACE_KEY])) { $class = $params[self::CONTROLLER_NAMESPACE_KEY] . '\\' . $params['controller']; $namespace = $params[self::CONTROLLER_NAMESPACE_KEY]; } else { if (Text::left($class, $namespace) != $namespace) { $parts = explode('\\', $class); array_pop($parts); $namespace = implode('\\', $parts); } $class = str_replace('\\', ':', $class); } $controller = strtolower(Text::offsetLeft($class, Text::len($namespace) + 1)); // possibility for sub controllers // sub controllers are seperated by colons in the url: foo:bar:qux will resolve into the controller foo\bar\Qux $exploded = $origin = explode(':', $controller); $controllerClassName = Text::toCamelCase(array_pop($exploded), true); if (count($exploded) > 0) { $controllerClass = $namespace . '\\' . implode('\\', $exploded) . '\\' . $controllerClassName; } else { $controllerClass = $namespace . '\\' . $controllerClassName; } // controller exists? if (!class_exists($controllerClass, true)) { throw new Exception_UnknownControllerClass("Controller of type '{$controllerClass}' was not found"); } $controller = new $controllerClass($this); $controller->setOrigin($origin); // controller is of the correct type? if (!$controller instanceof Controller) { throw new Exception("Controller of type '{$controllerClass}' is not of type lean\\Controller'"); } $params = new \lean\util\Object($params); $this->setParams($params); $controller->init(); $this->dispatchAction($controller, $action); }; // register dispatch with lean $route = $this->slim()->router()->map($pattern, $dispatch); $route->name($name); call_user_func_array(array($route, 'setHttpMethods'), $methods); return $route; }
/** * @return \lean\Template */ protected function createView() { $action = \lean\Text::splitCamelCase($this->getAction()); $file = $this->getViewDirectory() . "/{$action}.php"; $view = new \lean\Template($file); return $view; }