Ejemplo n.º 1
0
 /**
  * Starts the application.
  */
 public function run()
 {
     try {
         $start = new DateTime();
         $name = Environment::getApplicationName() . ' ' . Environment::getApplicationVersion();
         $this->logger->log("%header\n", $name, str_repeat('-', strlen($name)) . "\n");
         $this->fireEvent('startup');
         if ($this->config->help) {
             // Print help if requested
             $this->printHelp();
         } else {
             // Scan and parse sources
             $this->parse();
             $this->logger->log("Generating to directory %value\n", $this->config->destination);
             // Wipeout the destination directory
             $this->wipeout();
             // Generate the API documentation
             $this->generate();
             // Prints the elapsed time
             $this->printElapsed($start, new DateTime());
         }
         $this->fireEvent('shutdown');
     } catch (Exception $e) {
         $this->fireEvent('error', $e);
     }
 }
Ejemplo n.º 2
0
 /**
  * Returns templates directory path.
  *
  * @return string
  */
 public static function getTemplatesDir()
 {
     return realpath(Environment::getRootDir() . '/templates');
 }
Ejemplo n.º 3
0
 /**
  * Creates ZIP archive.
  *
  * @return \ApiGen\Generator
  * @throws \RuntimeException If something went wrong.
  */
 private function generateArchive()
 {
     if (!extension_loaded('zip')) {
         throw new RuntimeException('Extension zip is not loaded');
     }
     $archive = new \ZipArchive();
     if (true !== $archive->open($this->getArchivePath(), \ZipArchive::CREATE)) {
         throw new RuntimeException('Could not open ZIP archive');
     }
     $archive->setArchiveComment(trim(sprintf('%s API documentation generated by %s %s on %s', $this->config->title, Environment::getApplicationName(), Environment::getApplicationVersion(), date('Y-m-d H:i:s'))));
     $directory = Nette\Utils\Strings::webalize(trim(sprintf('%s API documentation', $this->config->title)), null, false);
     $destinationLength = strlen($this->config->destination);
     foreach ($this->getGeneratedFiles() as $file) {
         if (is_file($file)) {
             $archive->addFile($file, $directory . DIRECTORY_SEPARATOR . substr($file, $destinationLength + 1));
         }
     }
     if (false === $archive->close()) {
         throw new RuntimeException('Could not save ZIP archive');
     }
     $this->fireEvent('generateProgress', 1);
     return $this;
 }
Ejemplo n.º 4
0
 * ApiGen 3.0dev - API documentation generator for PHP 5.3+
 *
 * Copyright (c) 2010-2011 David Grudl (http://davidgrudl.com)
 * Copyright (c) 2011-2012 Jaroslav Hanslík (https://github.com/kukulich)
 * Copyright (c) 2011-2012 Ondřej Nešpor (https://github.com/Andrewsville)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */
namespace ApiGen;

use Nette\Diagnostics\Debugger;
// Check environment
try {
    require 'ApiGen/Environment.php';
    Environment::init();
} catch (\Exception $e) {
    fputs(STDERR, $e->getMessage() . "\n");
    exit(min(1, $e->getCode()));
}
// Init Nette debugger
Debugger::$strictMode = true;
Debugger::$onFatalError[] = function () {
    echo "\nFor more information turn on the debug mode using the --debug option.\n";
};
Debugger::enable(Debugger::PRODUCTION, false);
// Parse console input
$parser = new ConsoleParser();
$arguments = $parser->parseArguments(array_slice($argv, 1));
// Build the DIC
$configurator = new Config\Configurator($arguments);
Ejemplo n.º 5
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));
             }
         }
     }
 }