} //setroutes // $di->set('router', function () // { // require __DIR__.'/../app/config/routes.php'; // return $router; // }); $usecdn = $config->resource->useCDN; $di->set('usecdn', function () use($usecdn) { return $usecdn; }); // setplugins if ($config->security->protectBackend) { $di->set("dispatcher", function () use($di) { $dispatcher = new Phalcon\Mvc\Dispatcher(); $eventsManager = $di->getShared("eventsManager"); $security = new Security($di); $eventsManager->attach('dispatch', $security); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); } $di->set('flash', function () { return new Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }); //cookie $di->set("cookies", function () { $cookies = new Phalcon\Http\Response\Cookies(); $cookies->useEncryption(false); return $cookies; });
public function build() { $options = $this->_options; $path = ''; if (isset($this->_options['directory'])) { if ($this->_options['directory']) { $path = $this->_options['directory'] . '/'; } } $name = $options['name']; $config = $this->_getConfig($path); if (!isset($config->database->adapter)) { throw new BuilderException("Adapter was not found in the config. Please specify a config varaible [database][adapter]"); } $adapter = ucfirst($config->database->adapter); $this->isSupportedAdapter($adapter); $di = new \Phalcon\DI\FactoryDefault(); $di->set('db', function () use($adapter, $config) { $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; $connection = new $adapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->name)); return $connection; }); if (isset($config->application->modelsDir)) { $options['modelsDir'] = $path . $config->application->modelsDir; } else { throw new BuilderException("The builder is unable to know where is the views directory"); } if (isset($config->application->controllersDir)) { $options['controllersDir'] = $path . $config->application->controllersDir; } else { throw new BuilderException("The builder is unable to know where is the controllers directory"); } if (isset($config->application->viewsDir)) { $options['viewsDir'] = $path . $config->application->viewsDir; } else { throw new BuilderException("The builder is unable to know where is the views directory"); } $options['manager'] = $di->getShared('modelsManager'); $options['className'] = Text::camelize($options['name']); $options['fileName'] = Text::uncamelize($options['className']); $modelBuilder = new \Phalcon\Builder\Model(array('name' => $name, 'schema' => $options['schema'], 'className' => $options['className'], 'fileName' => $options['fileName'], 'genSettersGetters' => $options['genSettersGetters'], 'directory' => $options['directory'], 'force' => $options['force'])); $modelBuilder->build(); $modelClass = Text::camelize($name); if (!class_exists($modelClass)) { require $config->application->modelsDir . '/' . $modelClass . '.php'; } $entity = new $modelClass(); $metaData = $di->getShared('modelsMetadata'); $attributes = $metaData->getAttributes($entity); $dataTypes = $metaData->getDataTypes($entity); $identityField = $metaData->getIdentityField($entity); $primaryKeys = $metaData->getPrimaryKeyAttributes($entity); $setParams = array(); $selectDefinition = array(); $relationField = ''; $single = $name; $options['name'] = strtolower(Text::camelize($single)); $options['plural'] = str_replace('_', ' ', $single); $options['single'] = str_replace('_', ' ', $single); $options['entity'] = $entity; $options['theSingle'] = $single; $options['singleVar'] = $single; $options['setParams'] = $setParams; $options['attributes'] = $attributes; $options['dataTypes'] = $dataTypes; $options['primaryKeys'] = $primaryKeys; $options['identityField'] = $identityField; $options['relationField'] = $relationField; $options['selectDefinition'] = $selectDefinition; $options['autocompleteFields'] = array(); $options['belongsToDefinitions'] = array(); //Build Controller $this->_makeController($path, $options); //View layouts $this->_makeLayouts($path, $options); //View index.phtml $this->_makeViewIndex($path, $options); //View search.phtml $this->_makeViewSearch($path, $options); //View new.phtml $this->_makeViewNew($path, $options); //View edit.phtml $this->_makeViewEdit($path, $options); return true; }
return $flash; }; $di['flashSession'] = function () { $flash = new \Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning')); return $flash; }; $di['session'] = function () { $session = new Session(); $session->start(); return $session; }; $di['forms'] = function () { $formsManager = new FormsManager(); $forms = (array) @(include __DIR__ . '/forms.php'); foreach ($forms as $name => $form) { $formsManager->set($name, $form); } return $formsManager; }; $di['dispatcher'] = function () use($di) { /** @var \Phalcon\Events\Manager $eventsManager */ $eventsManager = $di->getShared('eventsManager'); $eventsManager->attach('dispatch', $di->get('exceptionPlugin')); $eventsManager->attach('dispatch', $di->get('authPlugin')); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }; $di['exceptionPlugin'] = '\\Plugin\\Exception'; $di['authPlugin'] = ['className' => '\\Plugin\\Auth', 'arguments' => [['type' => 'service', 'name' => 'acl']]]; return $di;