Beispiel #1
0
 /**
  * Internal building method builds all static services and some dynamic
  * services from file annotations, otherwise swagger info is loaded from
  * database or storage files for each service, if it exists.
  *
  * @throws \Exception
  */
 protected static function buildEventMaps()
 {
     \Log::info('Building event cache');
     //  Build event mapping from services in database
     //	Initialize the event map
     $processEventMap = [];
     $broadcastEventMap = [];
     //  Pull any custom swagger docs
     $result = ServiceModel::with(['serviceDocs' => function ($query) {
         $query->where('format', ApiDocFormatTypes::SWAGGER);
     }])->get();
     //	Spin through services and pull the events
     foreach ($result as $service) {
         $apiName = $service->name;
         try {
             if (empty($content = ServiceModel::getStoredContentForService($service))) {
                 throw new \Exception('  * No event content found for service.');
                 continue;
             }
             $serviceEvents = static::parseSwaggerEvents($apiName, $content);
             //	Parse the events while we get the chance...
             $processEventMap[$apiName] = ArrayUtils::get($serviceEvents, 'process', []);
             $broadcastEventMap[$apiName] = ArrayUtils::get($serviceEvents, 'broadcast', []);
             unset($content, $service, $serviceEvents);
         } catch (\Exception $ex) {
             \Log::error("  * System error building event map for service '{$apiName}'.\n{$ex->getMessage()}");
         }
     }
     static::$eventMap = ['process' => $processEventMap, 'broadcast' => $broadcastEventMap];
     //	Write event cache file
     \Cache::forever(static::EVENT_CACHE_KEY, static::$eventMap);
     \Log::info('Event cache build process complete');
 }
Beispiel #2
0
 /**
  * Main retrieve point for each service
  *
  * @param string $name Which service (name) to retrieve.
  *
  * @return string
  * @throws NotFoundException
  */
 public function getSwaggerForService($name)
 {
     $cachePath = $name . '.json';
     if (null === ($content = \Cache::get($cachePath))) {
         $service = Service::whereName($name)->get()->first();
         if (empty($service)) {
             throw new NotFoundException("Service '{$name}' not found.");
         }
         $content = ['swaggerVersion' => static::SWAGGER_VERSION, 'apiVersion' => \Config::get('df.api_version', static::API_VERSION), 'basePath' => url('/api/v2')];
         try {
             $result = Service::getStoredContentForService($service);
             if (empty($result)) {
                 throw new NotFoundException("No Swagger content found.");
             }
             $content = array_merge($content, $result);
             $content = json_encode($content, JSON_UNESCAPED_SLASHES);
             // replace service type placeholder with api name for this service instance
             $content = str_replace('{api_name}', $name, $content);
             // cache it for later access
             \Cache::forever($cachePath, $content);
         } catch (\Exception $ex) {
             \Log::error("  * System error creating swagger file for service '{$name}'.\n{$ex->getMessage()}");
         }
     }
     return $content;
 }