Ejemplo n.º 1
0
 /**
  * Checks ApiGen configuration.
  *
  * @param array $config Parsed configuration
  * @throws \ApiGen\Config\Exception If there is an error in configuration
  */
 private function checkConfiguration(array $config)
 {
     // Base configuration
     if (empty($config['source'])) {
         throw new ConfigException('Source is not set');
     }
     foreach ($config['source'] as $source) {
         if (!file_exists($source)) {
             throw new ConfigException(sprintf('Source "%s" doesn\'t exist', $source));
         }
     }
     if (empty($config['destination'])) {
         throw new ConfigException('Destination is not set');
     }
     foreach ($config['extensions'] as $extension) {
         if (!preg_match('~^[a-z\\d]+$~i', $extension)) {
             throw new ConfigException(sprintf('Invalid file extension "%s"', $extension));
         }
     }
     if (!empty($config['googleCseId']) && !preg_match('~^\\d{21}:[-a-z0-9_]{11}$~', $config['googleCseId'])) {
         throw new ConfigException(sprintf('Invalid Google Custom Search ID "%s"', $config['googleCseId']));
     }
     if (!empty($config['googleAnalytics']) && !preg_match('~^UA\\-\\d+\\-\\d+$~', $config['googleAnalytics'])) {
         throw new ConfigException(sprintf('Invalid Google Analytics tracking code "%s"', $config['googleAnalytics']));
     }
     if (empty($config['groups'])) {
         throw new ConfigException('No supported groups value given');
     }
     if (empty($config['autocomplete'])) {
         throw new ConfigException('No supported autocomplete value given');
     }
     if (empty($config['accessLevels'])) {
         throw new ConfigException('No supported access level given');
     }
     // Template configuration
     $require = $config['template']['require'];
     if (isset($require['min']) && !preg_match('~^\\d+(?:\\.\\d+){0,2}$~', $require['min'])) {
         throw new ConfigException(sprintf('Invalid minimal version definition "%s"', $require['min']));
     }
     if (isset($require['max']) && !preg_match('~^\\d+(?:\\.\\d+){0,2}$~', $require['max'])) {
         throw new ConfigException(sprintf('Invalid maximal version definition "%s"', $require['max']));
     }
     $isMinOk = function ($min) {
         $min .= str_repeat('.0', 2 - substr_count($min, '.'));
         return version_compare($min, Environment::getApplicationVersion(), '<=');
     };
     $isMaxOk = function ($max) {
         $max .= str_repeat('.0', 2 - substr_count($max, '.'));
         return version_compare($max, Environment::getApplicationVersion(), '>=');
     };
     if (isset($require['min'], $require['max']) && (!$isMinOk($require['min']) || !$isMaxOk($require['max']))) {
         throw new ConfigException(sprintf('The template requires version from "%s" to "%s", you are using version "%s"', $require['min'], $require['max'], Environment::getApplicationVersion()));
     } elseif (isset($require['min']) && !$isMinOk($require['min'])) {
         throw new ConfigException(sprintf('The template requires version "%s" or newer, you are using version "%s"', $require['min'], Environment::getApplicationVersion()));
     } elseif (isset($require['max']) && !$isMaxOk($require['max'])) {
         throw new ConfigException(sprintf('The template requires version "%s" or older, you are using version "%s"', $require['max'], Environment::getApplicationVersion()));
     }
     foreach (array('main', 'optional') as $section) {
         foreach ($config['template']['templates'][$section] as $type => $typeConfig) {
             if (!isset($typeConfig['filename'])) {
                 throw new ConfigException(sprintf('Filename for "%s" is not defined', $type));
             }
             if (!isset($typeConfig['template'])) {
                 throw new ConfigException(sprintf('Template for "%s" is not defined', $type));
             }
             if (null === Helper::getAbsoluteFilePath($typeConfig['template'], array(dirname($config['templateConfig'])))) {
                 throw new ConfigException(sprintf('Template for "%s" doesn\'t exist', $type));
             }
         }
     }
     // Plugins configuration
     foreach ($config['plugins'] as $pluginName => $definition) {
         if (!is_array($definition)) {
             throw new ConfigException(sprintf('Definition of plugin "%s" has to be an array', $pluginName));
         }
         if (!isset($definition['location'], $definition['class'])) {
             throw new ConfigException(sprintf('Plugin "%s" has to declare its location and class name', $pluginName));
         }
         foreach ($definition as $key => $value) {
             switch ($key) {
                 case 'location':
                 case 'class':
                     if (!is_string($value)) {
                         throw new ConfigException(sprintf('Parameter "%s" value has to be a string in plugin "%s" configuration', $key, $pluginName));
                     }
                     if ('location' === $key && !is_dir($value)) {
                         throw new ConfigException(sprintf('Plugin "%s" location "%s" does not exist', $pluginName, $value));
                     }
                     break;
                 case 'events':
                     if (!is_array($value)) {
                         throw new ConfigException(sprintf('Event hooks have to be defined as an array in plugin "%s" configuration', $pluginName));
                     }
                     foreach ($value as $index => $listenerDefinition) {
                         if (!preg_match(PluginsExtension::EVENT_LISTENER_FORMAT, $listenerDefinition, $matches)) {
                             throw new ConfigException(sprintf('Event hooks #%d definition is invalid in plugin "%s" configuration', $index + 1, $pluginName));
                         }
                     }
                     break;
                 case 'options':
                     if (!is_array($value)) {
                         throw new ConfigException(sprintf('Parameter "%s" value has to be an array in plugin "%s" configuration', $key, $pluginName));
                     }
                     break;
                 default:
                     throw new ConfigException(sprintf('Unknown plugin configuration option "%s" in plugin "%s" configuration', $key, $pluginName));
             }
         }
     }
 }