Skip to content

Instantly share code, notes, and snippets.

@nask0
Last active April 15, 2016 09:24
Show Gist options
  • Save nask0/f4d9d01b0b897f31fffd76df56e50ebd to your computer and use it in GitHub Desktop.
Save nask0/f4d9d01b0b897f31fffd76df56e50ebd to your computer and use it in GitHub Desktop.
class Api extends PhalconApplication
{
/* @var \Phalcon\Config\Adapter\Php */
protected $_config;
/* @var array - API endpoints that require no authentication */
protected $_publicEndpoints= [];
/* @var array - API endpoints that require signed Request Id to proceed */
protected $_protectedEndpoints = [];
public function setConfig( PhpConfig $config )
{
$this->_config = $config;
return $this;
}
/**
* Load services in application Dependency Injector
*/
public function loadServices()
{
/* @var \\Phalcon\DI\FactoryDefault */
$di = new DiFactoryDefault();
/* @var \Phalcon\Events\Manager */
$eventsManager = new EventsManager();
$di->set( 'config', $this->_config, true );
/* @var \CloseContacts\Lib\Http\Request */
$di->set( 'request', new HttpRequest(), true );
/* @var \CloseContacts\Lib\Http\Response*/
$di->set( 'response', new HttpResoponse(), true );
/* @var \Phalcon\Db\Adapter\Pdo\Mysql */
$dbAdapter = new MysqlAdapter([
'host' => $this->_config->database->host,
'username' => $this->_config->database->username,
'password' => $this->_config->database->password,
'dbname' => $this->_config->database->name
]);
$dbAdapter->setEventsManager( $eventsManager );
$di->set( 'db', $dbAdapter, true );
/* @var \Phalcon\Mvc\Model\Manager */
$modelsManager = new ModelsManager();
$modelsManager->setEventsManager( $eventsManager );
$modelsManager->registerNamespaceAlias( 'models', $this->_config->app->modelsNs );
$di->set( 'modelsManager', $modelsManager, true );
/* @var \Phalcon\Mvc\Dispatcher */
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager( $eventsManager );
$dispatcher->setDefaultNamespace( $this->_config->app->controllersNs );
$dispatcher->setDefaultController( 'error' );
$dispatcher->setDefaultAction( 'notFound' );
$di->set('dispatcher', $dispatcher, true);
/* @var \Phalcon\Mvc\Router */
$router = $this->_initRouter();
$router->setEventsManager( $eventsManager );
$di->set( 'router', $router, true );
/* @var \Phalcon\Mvc\Model\Transaction\Manager */
$di->set( 'transactions', new TransactionsManager(), true );
/* @var \CloseContacts\Lib\Application\Services\Validation */
$di->set( 'validation', new ValidationService( $this->_config->app->modelsNs ), true );
/* @var \CloseContacts\Lib\Application\Services\ClickaTell */
$di->set( 'clickatell', new ClickaTell() );
/* @var \CloseContacts\Lib\DataServices\DataServiceFactory */
$di->set( 'dataServices', new DataServiceFactory(), true );
$di->set( 'eventsManager', $eventsManager, true );
// disable view service since this is JSON API
$this->useImplicitView( false );
$this->setEventsManager( $eventsManager );
$this->setDI( $di );
return $this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment