public function init() { $app = $this->getApp(); $this->prependOptions(['path' => $app->getOption('path') . '/controllers', 'nameSpace' => null, 'loadPattern' => null, 'classNamePattern' => '%sController', 'methodNamePattern' => '%sAction', 'args' => [], 'helpers' => [], 'createLoader' => true, 'errorController' => 'error']); $app->bind('beforeRun', function () { $this->_initLoader(); $this->_initFactory(); $this->_initDispatcher(); $this->_instances = []; $this->_args = $this->getOption('args'); $this->_helpers = $this->getOption('helpers'); $this->registerHelper('dispatch', function ($controller, Request $request) { return $this->dispatch($request); }); $this->registerHelper('dispatchError', function ($controller, $action, $format = null, array $args = null) { if (isset($controller->request) && !$format) { $format = $controller->request->getFormat(); } if (!$format) { throw new \Exception("Failed to dispatch error: No format given"); } return $this->dispatchError($action, $format, $args); }); }); $app->bind('afterRun', function () { if ($this->_loader) { $this->_loader->unregister(); } unset($this->_loader); unset($this->_factory); unset($this->_dispatcher); unset($this->_instances); }); }
public function init() { if (!class_exists('Tale\\Data\\Source')) { throw new \RuntimeException("Failed to load data feature: " . "The data source class wasnt found. " . "Maybe you need the Tale\\Data namespace?"); } $app = $this->getApp(); $app->bind('beforeRun', function () use($app) { if (!isset($this->data)) { throw new \RuntimeException("Failed to initialize model feature: " . "The data feature is required"); } /** * @var \Tale\App\Feature\Data $data */ $data = $this->data; $this->_loaders = []; foreach ($this->getConfig() as $alias => $options) { $options = array_replace(['path' => $app->getOption('path') . '/models', 'nameSpace' => null, 'loadPattern' => null, 'createLoader' => true, 'source' => null, 'database' => null], $options ? $options : []); if ($options['createLoader']) { $loader = new ClassLoader($options['path'], $options['nameSpace'], $options['loadPattern']); $loader->register(); $this->_loaders[] = $loader; } if (!$options['source']) { throw new \RuntimeException("Failed to set model source: No source given"); } if (!$options['database']) { throw new \RuntimeException("Failed to set model database: No database given"); } $source = $data->getSource($options['source']); /** @var \Tale\Data\Database $database */ $database = $source->{$options['database']}; if ($options['nameSpace']) { $database->addModelNameSpace($options['nameSpace']); } if (isset($this->controller)) { $this->controller->setArg($alias, $database); } } }); $app->bind('afterRun', function () { foreach ($this->_loaders as $loader) { $loader->unregister(); } unset($this->_loaders); }); }