Автор: Nick Sagona, III (nick@popphp.org)
Пример #1
0
 /**
  * Load a project config
  *
  * @param  mixed $config
  * @throws Exception
  * @return \Pop\Project\Project
  */
 public function loadConfig($config)
 {
     // Test to see if the config is already set and changes are allowed.
     if (null !== $this->config && !$this->config->changesAllowed()) {
         throw new Exception('Real-time configuration changes are not allowed.');
     }
     // Else, set the new config
     if (is_array($config)) {
         $this->config = new Config($config);
     } else {
         if ($config instanceof Config) {
             $this->config = $config;
         } else {
             throw new Exception('The project config must be either an array or an instance of Pop\\Config.');
         }
     }
     return $this;
 }
Пример #2
0
<?php

require_once '../../bootstrap.php';
use Pop\Config;
try {
    $cfg1 = array('db' => array('name' => 'testdb', 'host' => 'localhost', 'user' => array('username' => 'testuser', 'password' => '12test34', 'role' => 'editor')), 'nav' => array('some' => 'nav'), 'module' => 'TestModule', 'oldvalue' => 123456);
    $cfg2 = array('db' => array('name' => 'testdb123', 'host' => 'localhost', 'user' => array('username' => 'testuser2', 'password' => '45test67', 'role' => 'editor')), 'nav' => array('some' => 'nav12'), 'module' => 'TestModule', 'newvalue' => array('Some new value'));
    $config1 = new Config($cfg1);
    $config2 = new Config($cfg2);
    $config1->merge($config2);
    print_r($config1);
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}
Пример #3
0
 /**
  * Get available model objects
  *
  * @param  \Pop\Config $config
  * @return array
  */
 public static function getModels($config = null)
 {
     $models = array('0' => '----');
     $exclude = array();
     $override = null;
     // Get any exclude or override config values
     if (null !== $config) {
         $configAry = $config->asArray();
         if (isset($configAry['exclude_models'])) {
             $exclude = $configAry['exclude_models'];
         }
         if (isset($configAry['override'])) {
             $override = $configAry['override'];
         }
     }
     // If override, set overridden models
     if (null !== $override) {
         foreach ($override as $model) {
             $models[$model] = $model;
         }
         // Else, get all modules from the system and module directories
     } else {
         $systemDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . '/vendor'), true);
         $sysModuleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . '/module'), true);
         $moduleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'), true);
         $dirs = array_merge($systemDirectory->getFiles(), $sysModuleDirectory->getFiles(), $moduleDirectory->getFiles());
         sort($dirs);
         // Dir clean up
         foreach ($dirs as $key => $dir) {
             unset($dirs[$key]);
             if (!(strpos($dir, 'PopPHPFramework') !== false || strpos($dir, 'config') !== false || strpos($dir, 'index.html') !== false)) {
                 $k = $dir;
                 if (substr($dir, -1) == DIRECTORY_SEPARATOR) {
                     $k = substr($k, 0, -1);
                 }
                 $k = substr($k, strrpos($k, DIRECTORY_SEPARATOR) + 1);
                 $dirs[$k] = $dir;
             }
         }
         // Loop through each directory, looking for model class files
         foreach ($dirs as $mod => $dir) {
             if (file_exists($dir . 'src/' . $mod . '/Model')) {
                 $d = new Dir($dir . 'src/' . $mod . '/Model');
                 $dFiles = $d->getFiles();
                 sort($dFiles);
                 foreach ($dFiles as $m) {
                     if (substr($m, 0, 8) !== 'Abstract') {
                         $model = str_replace('.php', '', $mod . '\\Model\\' . $m);
                         $wildcardModel = '*' . substr($model, strpos($model, '\\'));
                         if (!in_array($model, $exclude) && !in_array($wildcardModel, $exclude) && strpos($model, 'index.html') === false) {
                             $models[$model] = strpos($model, '\\') !== false ? substr($model, strrpos($model, '\\') + 1) : $model;
                         }
                     }
                 }
             }
         }
     }
     return $models;
 }
Пример #4
0
 /**
  * Static method to get model types
  *
  * @param  \Pop\Config $config
  * @return array
  */
 public static function getResources($config = null)
 {
     $resources = array();
     $exclude = array();
     $override = null;
     // Get any exclude or override config values
     if (null !== $config) {
         $configAry = $config->asArray();
         if (isset($configAry['exclude_controllers'])) {
             $exclude = $configAry['exclude_controllers'];
         }
         if (isset($configAry['override'])) {
             $override = $configAry['override'];
         }
     }
     // If override, set overridden resources
     if (null !== $override) {
         foreach ($override as $resource) {
             $resources[] = $resource;
         }
         // Else, get all controllers from the system and module directories
     } else {
         $systemDirectory = new Dir(realpath(__DIR__ . '/../../../../'), true);
         $systemModuleDirectory = new Dir(realpath(__DIR__ . '/../../../../../module/'), true);
         $moduleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'), true);
         $dirs = array_merge($systemDirectory->getFiles(), $systemModuleDirectory->getFiles(), $moduleDirectory->getFiles());
         sort($dirs);
         // Dir clean up
         foreach ($dirs as $key => $dir) {
             unset($dirs[$key]);
             if (!(strpos($dir, 'config') !== false || strpos($dir, 'index.html') !== false)) {
                 $k = $dir;
                 if (substr($dir, -1) == DIRECTORY_SEPARATOR) {
                     $k = substr($k, 0, -1);
                 }
                 $k = substr($k, strrpos($k, DIRECTORY_SEPARATOR) + 1);
                 $dirs[$k] = $dir;
             }
         }
         // Loop through each directory, looking for controller class files
         foreach ($dirs as $mod => $dir) {
             if (file_exists($dir . 'src/' . $mod . '/Controller')) {
                 $d = new Dir($dir . 'src/' . $mod . '/Controller', true, true, false);
                 $dFiles = $d->getFiles();
                 sort($dFiles);
                 // If found, loop through the files, getting the methods as the "permissions"
                 foreach ($dFiles as $c) {
                     if (strpos($c, 'index.html') === false && strpos($c, 'Abstract') === false) {
                         // Get all public methods from class
                         $class = str_replace(array('.php', DIRECTORY_SEPARATOR), array('', '\\'), substr($c, strpos($c, 'src') + 4));
                         $code = new \ReflectionClass($class);
                         $methods = $code->getMethods(\ReflectionMethod::IS_PUBLIC);
                         $actions = array();
                         foreach ($methods as $value) {
                             if ($value->getName() !== '__construct' && $value->class == $class) {
                                 $action = $value->getName();
                                 if (!isset($exclude[$class]) || isset($exclude[$class]) && is_array($exclude[$class]) && !in_array($action, $exclude[$class])) {
                                     $actions[] = $action;
                                 }
                             }
                         }
                         $types = array(0 => '(All)');
                         if (strpos($class, "\\Controller\\IndexController") === false) {
                             $classAry = explode('\\', $class);
                             $end1 = count($classAry) - 2;
                             $end2 = count($classAry) - 1;
                             $model = $classAry[0] . '_Model_';
                             if (stripos($classAry[$end2], 'index') !== false) {
                                 $model .= $classAry[$end1];
                             } else {
                                 if (substr($classAry[$end2], 0, 4) == 'Type') {
                                     $model .= $classAry[$end1] . 'Type';
                                 } else {
                                     $model .= str_replace('Controller', '', $classAry[$end2]);
                                 }
                             }
                             if (substr($model, -3) == 'ies') {
                                 $model = substr($model, 0, -3) . 'y';
                             } else {
                                 if (substr($model, -1) == 's') {
                                     $model = substr($model, 0, -1);
                                 }
                             }
                             $types = \Phire\Project::getModelTypes($model);
                             // Format the resource and permissions
                             $c = str_replace(array('Controller.php', '\\'), array('', '/'), $c);
                             $c = substr($c, strpos($c, 'Controller') + 11);
                             $c = str_replace('Phire/', '', $c);
                             if (!in_array($class, $exclude) || isset($exclude[$class]) && is_array($exclude[$class])) {
                                 $resources[$class] = array('name' => $c, 'types' => $types, 'actions' => $actions);
                             }
                         }
                     }
                 }
             }
         }
     }
     return $resources;
 }
Пример #5
0
 public function testAsArrayObject()
 {
     $c = new Config(array('data' => 123));
     $this->assertInstanceOf('ArrayObject', $c->asArrayObject());
 }