Пример #1
0
 public function testParseFromPathTestingArrays()
 {
     $file = dirname(dirname(dirname(__DIR__))) . DS . 'data' . DS . 'CsvMigrations' . DS . 'migrations' . DS . 'Foo' . DS . 'array_in_config.ini';
     $parser = new Parser();
     $result = $parser->parseFromPath($file);
     $this->assertTrue(is_array($result), 'Return data from parser isn\'t array type');
     $this->assertArrayHasKey('associations', $result, "No associations found in the table config");
     $this->assertArrayHasKey('association_labels', $result['associations'], "No associations found in the table config");
     $this->assertTrue(is_array($result['associations']['association_labels']), "Associations label is not an array");
 }
 /**
  * Method that sets table configuration.
  *
  * @param string $tableName table name
  * @return void
  */
 protected function _setConfiguration($tableName)
 {
     $pathFinder = new ConfigPathFinder();
     $path = $pathFinder->find(Inflector::camelize($tableName));
     $parser = new Parser();
     $this->_config = $parser->parseFromPath($path);
     // display field from configuration file
     if (isset($this->_config['table']['display_field']) && method_exists($this, 'displayField')) {
         $this->displayField($this->_config['table']['display_field']);
     }
     // module icon from configuration file
     if (isset($this->_config['table']['icon'])) {
         $this->icon($this->_config['table']['icon']);
     }
     // lookup field(s) from configuration file
     if (isset($this->_config['table']['lookup_fields'])) {
         $this->lookupFields($this->_config['table']['lookup_fields']);
     }
     // typeahead field(s) from configuration file
     if (isset($this->_config['table']['typeahead_fields'])) {
         $this->typeaheadFields($this->_config['table']['typeahead_fields']);
     }
     // set module alias from configuration file
     if (isset($this->_config['table']['alias'])) {
         $this->moduleAlias($this->_config['table']['alias']);
     }
     if (isset($this->_config['table']['allow_reminders'])) {
         $this->tableSection($this->_config['table']);
     }
     // set searchable flag from configuration file
     if (isset($this->_config['table']['searchable'])) {
         $this->isSearchable($this->_config['table']['searchable']);
     }
     if (isset($this->_config['associations']['hide_associations'])) {
         $this->hiddenAssociations($this->_config['associations']['hide_associations']);
     }
     if (isset($this->_config['associationLabels'])) {
         $this->associationLabels($this->_config['associationLabels']);
     }
     if (isset($this->_config['parent'])) {
         $this->parentSection($this->_config['parent']);
     }
     if (isset($this->_config['notifications'])) {
         $this->notifications($this->_config['notifications']);
     }
     // set virtual field(s)
     if (isset($this->_config['virtualFields'])) {
         $this->setVirtualFields($this->_config['virtualFields']);
     }
 }
 /**
  * Check configuration options for each module
  *
  * @param array $modules List of modules to check
  * @return int Count of errors found
  */
 protected function _checkConfigOptions(array $modules = [])
 {
     $errors = [];
     $warnings = [];
     $this->out('Checking configuration options:', 2);
     foreach ($modules as $module => $path) {
         $moduleErrors = [];
         $this->out(' - ' . $module . ' ... ', 0);
         $config = null;
         try {
             $pathFinder = new ConfigPathFinder();
             $path = $pathFinder->find($module);
             $parser = new Parser();
             $config = $parser->parseFromPath($path);
         } catch (\Exception $e) {
             // We've already reported this problem in _checkConfigPresence();
         }
         // Check configuration options
         if ($config) {
             // [table] section
             if (!empty($config['table'])) {
                 // 'display_field' key is optional, but must contain valid field if specified
                 if (!empty($config['table']['display_field'])) {
                     if (!$this->_isValidModuleField($module, $config['table']['display_field'])) {
                         $moduleErrors[] = $module . " config [table] section references unknown field '" . $config['table']['display_field'] . "' in 'display_field' key";
                     }
                 } else {
                     $warnings[] = $module . " config [table] section does not specify 'display_field' key";
                 }
                 // 'icon' key is optional, but strongly suggested
                 if (empty($config['table']['icon'])) {
                     $warnings[] = $module . " config [table] section does not specify 'icon' key";
                 }
                 // 'typeahead_fields' key is optional, but must contain valid fields if specified
                 if (!empty($config['table']['typeahead_fields'])) {
                     $typeaheadFields = explode(',', trim($config['table']['typeahead_fields']));
                     foreach ($typeaheadFields as $typeaheadField) {
                         if (!$this->_isValidModuleField($module, $typeaheadField)) {
                             $moduleErrors[] = $module . " config [table] section references unknown field '" . $typeaheadField . "' in 'typeahead_fields' key";
                         }
                     }
                 }
                 // 'lookup_fields' key is optional, but must contain valid fields if specified
                 if (!empty($config['table']['lookup_fields'])) {
                     $lookupFields = explode(',', $config['table']['lookup_fields']);
                     foreach ($lookupFields as $lookupField) {
                         if (!$this->_isValidModuleField($module, $lookupField)) {
                             $moduleErrors[] = $module . " config [table] section references unknown field '" . $lookupField . "' in 'lookup_fields' key";
                         }
                     }
                 }
             }
             // [parent] section
             if (!empty($config['parent'])) {
                 if (empty($config['parent']['module'])) {
                     $moduleErrors[] = $module . " config [parent] section is missing 'module' key";
                 }
                 if (!empty($config['parent']['module'])) {
                     if (!$this->_isValidModule($config['parent']['module'], array_keys($modules))) {
                         $moduleErrors[] = $module . " config [parent] section references unknown module '" . $config['parent']['module'] . "' in 'module' key";
                     }
                 }
                 if (!empty($config['parent']['relation'])) {
                     if (!$this->_isRealModuleField($config['parent']['relation'], $module)) {
                         $moduleErrors[] = $module . " config [parent] section references non-real field '" . $config['parent']['relation'] . "' in 'relation' key";
                     }
                 }
                 if (!empty($config['parent']['redirect'])) {
                     if (!in_array($config['parent']['redirect'], ['self', 'parent'])) {
                         $moduleErrors[] = $module . " config [parent] section references unknown redirect type '" . $config['parent']['redirect'] . "' in 'redirect key";
                     }
                     //if redirect = parent, we force the user to mention the relation and module
                     if (in_array($config['parent']['redirect'], ['parent'])) {
                         if (empty($config['parent']['module'])) {
                             $moduleErrors[] = $module . " config [parent] requires 'module' value when redirect = parent.";
                         }
                         if (empty($config['parent']['relation'])) {
                             $moduleErrors[] = $module . " config [parent] requires 'relation' when redirect = parent.";
                         }
                     }
                 }
             }
             // [virtualFields] section
             if (!empty($config['virtualFields'])) {
                 foreach ($config['virtualFields'] as $virtualField => $realFields) {
                     $realFieldsList = explode(',', $realFields);
                     if (empty($realFieldsList)) {
                         $moduleErrors[] = $module . " config [virtualFields] section does not define real fields for '{$virtualField}' virtual field";
                         continue;
                     }
                     foreach ($realFieldsList as $realField) {
                         if (!$this->_isRealModuleField($module, $realField)) {
                             $moduleErrors[] = $module . " config [virtualFields] section uses a non-real field in '{$virtualField}' virtual field";
                         }
                     }
                 }
             }
             // [manyToMany] section
             if (!empty($config['manyToMany'])) {
                 // 'module' key is required and must contain valid modules
                 if (empty($config['manyToMany']['modules'])) {
                     $moduleErrors[] = $module . " config [manyToMany] section is missing 'modules' key";
                 } else {
                     $manyToManyModules = explode(',', $config['manyToMany']['modules']);
                     foreach ($manyToManyModules as $manyToManyModule) {
                         if (!$this->_isValidModule($manyToManyModule, array_keys($modules))) {
                             $moduleErrors[] = $module . " config [manyToMany] section references unknown module '{$manyToManyModule}' in 'modules' key";
                         }
                     }
                 }
             }
             // [notifications] section
             if (!empty($config['notifications'])) {
                 // 'ignored_fields' key is optional, but must contain valid fields if specified
                 if (!empty($config['notifications']['ignored_fields'])) {
                     $ignoredFields = explode(',', trim($config['notifications']['ignored_fields']));
                     foreach ($ignoredFields as $ignoredField) {
                         if (!$this->_isValidModuleField($module, $ignoredField)) {
                             $moduleErrors[] = $module . " config [notifications] section references unknown field '" . $ignoredField . "' in 'typeahead_fields' key";
                         }
                     }
                 }
             }
             // [conversion] section
             if (!empty($config['conversion'])) {
                 // 'module' key is required and must contain valid modules
                 if (empty($config['conversion']['modules'])) {
                     $moduleErrors[] = $module . " config [conversion] section is missing 'modules' key";
                 } else {
                     $conversionModules = explode(',', $config['conversion']['modules']);
                     foreach ($conversionModules as $conversionModule) {
                         // Only check for simple modules, not the vendor/plugin ones
                         if (preg_match('/^\\w+$/', $conversionModule) && !$this->_isValidModule($conversionModule, array_keys($modules))) {
                             $moduleErrors[] = $module . " config [conversion] section references unknown module '{$conversionModule}' in 'modules' key";
                         }
                     }
                 }
                 // 'inherit' key is optional, but must contain valid modules if defined
                 if (!empty($config['conversion']['inherit'])) {
                     $inheritModules = explode(',', $config['conversion']['inherit']);
                     foreach ($inheritModules as $inheritModule) {
                         if (!$this->_isValidModule($inheritModule, array_keys($modules))) {
                             $moduleErrors[] = $module . " config [conversion] section references unknown module '{$inheritModule}' in 'inherit' key";
                         }
                     }
                 }
                 // 'field' key is optional, but must contain valid field and 'value' if defined
                 if (!empty($config['conversion']['field'])) {
                     // 'field' key is optional, but must contain valid field is specified
                     if (!$this->_isValidModuleField($module, $config['conversion']['field'])) {
                         $moduleErrors[] = $module . " config [conversion] section references unknown field '" . $config['conversion']['field'] . "' in 'field' key";
                     }
                     // 'value' key must be set
                     if (!isset($config['conversion']['value'])) {
                         $moduleErrors[] = $module . " config [conversion] section references 'field' but does not set a 'value' key";
                     }
                 }
             }
         }
         $result = empty($moduleErrors) ? '<success>OK</success>' : '<error>FAIL</error>';
         $this->out($result);
         $errors = array_merge($errors, $moduleErrors);
     }
     $this->_printCheckStatus($errors, $warnings);
     return count($errors);
 }
 /**
  * Used in <model>/report/<slug> method
  * to get reports from the ini file on the dynamic
  * model/table.
  * @return array $config containing all reports from ini files
  */
 public function _getReports()
 {
     $result = $config = [];
     $filename = Configure::read('CsvMigrations.reports.filename');
     $filename .= '.ini';
     $path = Configure::read('CsvMigrations.migrations.path');
     $dir = new \DirectoryIterator($path);
     foreach ($dir as $it) {
         if ($it->isDir() && !$it->isDot()) {
             $subDir = new \DirectoryIterator($it->getPathname());
             foreach ($subDir as $fileInfo) {
                 if ($fileInfo->isFile() && $filename === $fileInfo->getFilename()) {
                     $result[$it->getFilename()][] = $fileInfo->getPathname();
                 }
             }
         }
     }
     if (!empty($result)) {
         $parser = new Parser();
         foreach ($result as $model => $paths) {
             foreach ($paths as $p) {
                 $config[$model] = $parser->parseFromPath($p);
             }
         }
     }
     return $config;
 }