Exemple #1
0
 /**
  * @param Config $config
  */
 public function __construct(Config $config)
 {
     $this->redirectUriAuthorize = $config->get('redirectUri');
     $this->clientId = $config->get('clientId');
     $this->clientSecret = $config->get('clientSecret');
     $this->logger = $this->getDI()->get('logger', ['auth.log']);
 }
 /**
  * Resolves the DB Schema
  *
  * @param \Phalcon\Config $config
  * @return null|string
  */
 public static function resolveDbSchema(Config $config)
 {
     if ($config->offsetExists('schema')) {
         return $config->get('schema');
     }
     if (self::DB_ADAPTER_POSTGRESQL == $config->get('adapter')) {
         return 'public';
     }
     if ($config->offsetExists('dbname')) {
         return $config->get('dbname');
     }
     return null;
 }
Exemple #3
0
 /**
  * Runs the application, performing all initializations
  *
  * @return void
  */
 public function run()
 {
     $modules = $this->_config->get('modules');
     if (!$modules) {
         $modules = (object) [];
     }
     $di = $this->_dependencyInjector;
     $config = $this->_config;
     if (isset($this->_config->application)) {
         if ($this->_config->application->debug) {
             if (!isset($this->_config->application->useCachingInDebugMode)) {
                 $this->_config->application->useCachingInDebugMode = false;
             }
         }
     }
     $di->set('modules', function () use($modules) {
         return $modules;
     });
     // Set application event manager
     $eventsManager = new \Phalcon\Events\Manager();
     // Init services and engine system
     foreach ($this->_services as $serviceName) {
         $service = $this->_serviceNamespace . "\\" . ucfirst($serviceName);
         $service = new $service($di, $eventsManager, $config);
         if (!$service instanceof \Engine\Application\Service\AbstractService) {
             throw new \Engine\Exception("Service '{$serviceName}' not instance of AbstractService");
         }
         $service->init();
     }
     // register enabled modules
     $enabledModules = [];
     if (!empty($modules)) {
         foreach ($modules as $module => $enabled) {
             if (!$enabled) {
                 continue;
             }
             $moduleName = ucfirst($module);
             $enabledModules[$module] = array('className' => $moduleName . '\\Module', 'path' => ROOT_PATH . '/apps/modules/' . $moduleName . '/Module.php');
         }
         if (!empty($enabledModules)) {
             $this->registerModules($enabledModules);
         }
     }
     // Set default services to the DI
     $this->setEventsManager($eventsManager);
     $di->setShared('eventsManager', $eventsManager);
     $di->setShared('app', $this);
 }
Exemple #4
0
 public function testFactoryShouldWorkIfCreatedFromConfigPHPArray()
 {
     $config = new Config(include INCUBATOR_FIXTURES . 'Acl/acl.php');
     $factory = new MemoryFactory();
     $acl = $factory->create($config->get('acl'));
     $this->assertInstanceOf('Phalcon\\Acl\\Adapter\\Memory', $acl);
     $this->assertAclIsConfiguredAsExpected($acl, $config);
 }
Exemple #5
0
 /**
  * Resolves the DB Schema
  *
  * @param \Phalcon\Config $config
  * @return null|string
  */
 public static function resolveDbSchema(Config $config)
 {
     if ($config->offsetExists('schema')) {
         return $config->get('schema');
     }
     if (self::DB_ADAPTER_POSTGRESQL == $config->get('adapter')) {
         return 'public';
     }
     if (self::DB_ADAPTER_SQLITE == $config->get('adapter')) {
         // SQLite only supports the current database, unless one is
         // attached. This is not the case, so don't return a schema.
         return null;
     }
     if ($config->offsetExists('dbname')) {
         return $config->get('dbname');
     }
     return null;
 }
 public static function setCurrentVersion($lastVersion, $currentVersion)
 {
     if (is_null(self::$_connection)) {
         self::connSetup(self::$_config->get('database'));
     }
     if (!is_null(self::$_config->get('migrationsLog')) && 'database' == self::$_config->get('migrationsLog')) {
         self::$_connection->execute('UPDATE `phalcon_migrations` SET `version`="' . (string) $currentVersion . '" WHERE `version`="' . (string) $lastVersion . '" LIMIT 1;');
     } else {
         file_put_contents(self::$_migrationFid, (string) $currentVersion);
     }
 }
Exemple #7
0
 /**
  * Index a single document
  *
  * @param Posts $post
  */
 protected function doIndex(Posts $post)
 {
     $params = [];
     $karma = $post->number_views + ($post->votes_up - $post->votes_down) * 10 + $post->number_replies;
     if ($karma > 0) {
         $params['body'] = ['id' => $post->id, 'title' => $post->title, 'category' => $post->categories_id, 'content' => $post->content, 'karma' => $karma];
         $params['index'] = $this->config->get('index', 'phosphorum');
         $params['type'] = 'post';
         $params['id'] = 'post-' . $post->id;
         $this->client->index($params);
     }
 }
Exemple #8
0
 protected function initLoader()
 {
     $autoload = static::$config->get('autoload');
     if ($autoload) {
         $loader = new Loader();
         if ($autoload->get('dir') instanceof Config) {
             $loader->registerDirs($autoload->get('dir')->toArray());
         }
         if ($autoload->get('namespace') instanceof Config) {
             $loader->registerNamespaces($autoload->get('namespace')->toArray());
         }
         $loader->register();
     }
 }
 /**
  * Returns database name
  *
  * @return mixed
  */
 public static function getDbName()
 {
     return self::$_databaseConfig->get('dbname');
 }
Exemple #10
0
 public function testMaintainConfigObjectReference()
 {
     $config = new Config(['basedir' => static::$basedir]);
     $b = new Bootstrap(self::getUserOptions(), $config, static::$di);
     $b->getConfig()->foo = 'bar';
     $this->assertEquals('bar', $config->get('foo'));
 }
 /**
  * Look for table definition modifications and apply to real table
  *
  * @param $tableName
  * @param $definition
  *
  * @throws \Phalcon\Db\Exception
  */
 public function morphTable($tableName, $definition)
 {
     $defaultSchema = self::$_databaseConfig->get('dbname');
     $tableExists = self::$_connection->tableExists($tableName, $defaultSchema);
     if (isset($definition['columns'])) {
         if (count($definition['columns']) == 0) {
             throw new DbException('Table must have at least one column');
         }
         $fields = array();
         foreach ($definition['columns'] as $tableColumn) {
             if (!is_object($tableColumn)) {
                 throw new DbException('Table must have at least one column');
             }
             /**
              * @var \Phalcon\Db\ColumnInterface $tableColumn
              * @var \Phalcon\Db\ColumnInterface[] $fields
              */
             $fields[$tableColumn->getName()] = $tableColumn;
         }
         if ($tableExists == true) {
             $localFields = array();
             /**
              * @var \Phalcon\Db\ColumnInterface[] $description
              * @var \Phalcon\Db\ColumnInterface[] $localFields
              */
             $description = self::$_connection->describeColumns($tableName, $defaultSchema);
             foreach ($description as $field) {
                 $localFields[$field->getName()] = $field;
             }
             foreach ($fields as $fieldName => $tableColumn) {
                 /**
                  * @var \Phalcon\Db\ColumnInterface $tableColumn
                  * @var \Phalcon\Db\ColumnInterface[] $localFields
                  */
                 if (!isset($localFields[$fieldName])) {
                     self::$_connection->addColumn($tableName, $tableColumn->getSchemaName(), $tableColumn);
                 } else {
                     $changed = false;
                     if ($localFields[$fieldName]->getType() != $tableColumn->getType()) {
                         $changed = true;
                     }
                     if ($localFields[$fieldName]->getSize() != $tableColumn->getSize()) {
                         $changed = true;
                     }
                     if ($tableColumn->isNotNull() != $localFields[$fieldName]->isNotNull()) {
                         $changed = true;
                     }
                     if ($tableColumn->getDefault() != $localFields[$fieldName]->getDefault()) {
                         $changed = true;
                     }
                     if ($changed == true) {
                         self::$_connection->modifyColumn($tableName, $tableColumn->getSchemaName(), $tableColumn);
                     }
                 }
             }
             foreach ($localFields as $fieldName => $localField) {
                 if (!isset($fields[$fieldName])) {
                     self::$_connection->dropColumn($tableName, null, $fieldName);
                 }
             }
         } else {
             self::$_connection->createTable($tableName, $defaultSchema, $definition);
             if (method_exists($this, 'afterCreateTable')) {
                 $this->afterCreateTable();
             }
         }
     }
     if (isset($definition['references'])) {
         if ($tableExists == true) {
             $references = array();
             foreach ($definition['references'] as $tableReference) {
                 $references[$tableReference->getName()] = $tableReference;
             }
             $localReferences = array();
             $activeReferences = self::$_connection->describeReferences($tableName, $defaultSchema);
             foreach ($activeReferences as $activeReference) {
                 $localReferences[$activeReference->getName()] = array('referencedTable' => $activeReference->getReferencedTable(), 'columns' => $activeReference->getColumns(), 'referencedColumns' => $activeReference->getReferencedColumns());
             }
             foreach ($definition['references'] as $tableReference) {
                 if (!isset($localReferences[$tableReference->getName()])) {
                     self::$_connection->addForeignKey($tableName, $tableReference->getSchemaName(), $tableReference);
                 } else {
                     $changed = false;
                     if ($tableReference->getReferencedTable() != $localReferences[$tableReference->getName()]['referencedTable']) {
                         $changed = true;
                     }
                     if ($changed == false) {
                         if (count($tableReference->getColumns()) != count($localReferences[$tableReference->getName()]['columns'])) {
                             $changed = true;
                         }
                     }
                     if ($changed == false) {
                         if (count($tableReference->getReferencedColumns()) != count($localReferences[$tableReference->getName()]['referencedColumns'])) {
                             $changed = true;
                         }
                     }
                     if ($changed == false) {
                         foreach ($tableReference->getColumns() as $columnName) {
                             if (!in_array($columnName, $localReferences[$tableReference->getName()]['columns'])) {
                                 $changed = true;
                                 break;
                             }
                         }
                     }
                     if ($changed == false) {
                         foreach ($tableReference->getReferencedColumns() as $columnName) {
                             if (!in_array($columnName, $localReferences[$tableReference->getName()]['referencedColumns'])) {
                                 $changed = true;
                                 break;
                             }
                         }
                     }
                     if ($changed == true) {
                         self::$_connection->dropForeignKey($tableName, $tableReference->getSchemaName(), $tableReference->getName());
                         self::$_connection->addForeignKey($tableName, $tableReference->getSchemaName(), $tableReference);
                     }
                 }
             }
             foreach ($localReferences as $referenceName => $reference) {
                 if (!isset($references[$referenceName])) {
                     self::$_connection->dropForeignKey($tableName, null, $referenceName);
                 }
             }
         }
     }
     if (isset($definition['indexes'])) {
         if ($tableExists == true) {
             $indexes = array();
             foreach ($definition['indexes'] as $tableIndex) {
                 $indexes[$tableIndex->getName()] = $tableIndex;
             }
             $localIndexes = array();
             $actualIndexes = self::$_connection->describeIndexes($tableName, $defaultSchema);
             foreach ($actualIndexes as $actualIndex) {
                 $localIndexes[$actualIndex->getName()] = $actualIndex->getColumns();
             }
             foreach ($definition['indexes'] as $tableIndex) {
                 if (!isset($localIndexes[$tableIndex->getName()])) {
                     if ($tableIndex->getName() == 'PRIMARY') {
                         self::$_connection->addPrimaryKey($tableName, $tableColumn->getSchemaName(), $tableIndex);
                     } else {
                         self::$_connection->addIndex($tableName, $tableColumn->getSchemaName(), $tableIndex);
                     }
                 } else {
                     $changed = false;
                     if (count($tableIndex->getColumns()) != count($localIndexes[$tableIndex->getName()])) {
                         $changed = true;
                     } else {
                         foreach ($tableIndex->getColumns() as $columnName) {
                             if (!in_array($columnName, $localIndexes[$tableIndex->getName()])) {
                                 $changed = true;
                                 break;
                             }
                         }
                     }
                     if ($changed == true) {
                         if ($tableIndex->getName() == 'PRIMARY') {
                             self::$_connection->dropPrimaryKey($tableName, $tableColumn->getSchemaName());
                             self::$_connection->addPrimaryKey($tableName, $tableColumn->getSchemaName(), $tableIndex);
                         } else {
                             self::$_connection->dropIndex($tableName, $tableColumn->getSchemaName(), $tableIndex->getName());
                             self::$_connection->addIndex($tableName, $tableColumn->getSchemaName(), $tableIndex);
                         }
                     }
                 }
             }
             foreach ($localIndexes as $indexName => $indexColumns) {
                 if (!isset($indexes[$indexName])) {
                     self::$_connection->dropIndex($tableName, null, $indexName);
                 }
             }
         }
     }
 }
Exemple #12
0
 /**
  * Initialize the Gravatar Service.
  *
  * @param DiInterface   $di     Dependency Injector
  * @param Config        $config App config
  * @param EventsManager $em     Events Manager
  *
  * @return void
  */
 protected function initGravatar(DiInterface $di, Config $config, EventsManager $em)
 {
     $di->setShared('gravatar', function () use($config) {
         return new Gravatar($config->get('gravatar', new Config()));
     });
 }
 protected function registerServices()
 {
     // todo: think about misambugous
     $loader = new ServiceLoader($this);
     $loader->load($this->config->get('services'));
 }
Exemple #14
0
 /**
  * Add role to acl.
  *
  * @param string          $role  role
  * @param \Phalcon\Config $rules rules
  *
  * @return $this
  *
  * @throws \Phalcon\Acl\Exception
  */
 protected function addRole($role, \Phalcon\Config $rules)
 {
     // role has inheritance ?
     if ($rules->get('inherit')) {
         // role exists?
         if (!array_key_exists($rules->inherit, $this->roles)) {
             throw new \Phalcon\Acl\Exception(sprintf('Role "%s" cannot inherit non-existent role "%s".
                  Either such role does not exist or it is set to be inherited before it is actually defined.', $role, $rules->inherit));
         }
         $this->acl->addRole($this->roles[$role], $this->roles[$rules->inherit]);
     } else {
         $this->acl->addRole($this->roles[$role]);
     }
     return $this;
 }
Exemple #15
0
 public function get($index, $defaultValue = null)
 {
     return parent::get($index, $defaultValue);
 }