/** * Constructor * * @param array $options */ public function __construct(array $options = array()) { if (!isset($options['name'])) { throw new \Exception('[Norm/Collection] Missing name, check collection configuration!'); } $this->clazz = Inflector::classify($options['name']); $this->name = Inflector::tableize($this->clazz); if (isset($options['connection'])) { $this->connection = $options['connection']; unset($options['connection']); $options['debug'] = $this->connection->option('debug') ? true : false; } if (isset($options['observers'])) { foreach ($options['observers'] as $Observer => $observerOptions) { if (is_int($Observer)) { $Observer = $observerOptions; $observerOptions = null; } if (is_string($Observer)) { $Observer = new $Observer($observerOptions); } $this->observe($Observer); } } if (isset($options['schema'])) { $this->schema = new Object($options['schema']); unset($options['schema']); } else { $this->schema = new Object(); } $this->options = $options; $this->applyHook('initialized', $this); $this->resetCache(); }
/** * Constructor * * @param [type] $app [description] * @param [type] $baseUri [description] */ public function __construct($app, $baseUri) { $this->app = $app; $this->request = $app->request; $this->response = $app->response; $this->baseUri = $baseUri; $exploded = explode('/', $baseUri); $clazz = $this->clazz = Inflector::classify(end($exploded)); $controller = $this; $response = $this->response; $app->filter('controller', function () use($controller) { return $controller; }); $app->filter('controller.name', function () use($clazz) { return $clazz; }); $app->filter('controller.uri', function ($uri) use($controller, $app) { if (strpos($uri, ':id')) { $params = $app->router->getCurrentRoute()->getParams(); $uri = str_replace(':id', $params['id'] ?: 'null', $uri); } return $controller->getBaseUri() . $uri; }); $app->filter('controller.url', function ($uri) use($controller, $app) { return URL::site(f('controller.uri', $uri)); }); $app->filter('controller.urlWithQuery', function ($uri) use($controller, $app) { return f('controller.url', $uri) . ($app->environment['QUERY_STRING'] ? '?' . $app->environment['QUERY_STRING'] : ''); }); $app->filter('controller.redirectUrl', function ($uri) use($controller) { return $controller->getRedirectUri(); }); $app->hook('bono.controller.before', function ($options) use($app, $controller, $response) { $template = $controller->getTemplate($options['method']); $response->template($template); }); $app->hook('bono.controller.after', function ($options) use($app, $controller, $response) { $response->data($controller->data()); }); $this->mapRoute(); }
protected function humanize($name) { return Inflector::classify($name); }
/** * Register a route model. * * @param string $key * * @return mixed */ public function routeModel($key) { if (!isset($this->routeModels[$key])) { $Clazz = Inflector::classify($key); $collection = Norm::factory($this->schema($key)->get('foreign')); $this->routeModels[$key] = $collection->findOne($this->routeData($key)); } return $this->routeModels[$key]; }