/**
  * Clean up abandoned assignments.
  */
 public function cleanup()
 {
     $this->model->leftJoin('streams_streams', 'streams_assignments.stream_id', '=', 'streams_streams.id')->leftJoin('streams_fields', 'streams_assignments.field_id', '=', 'streams_fields.id')->whereNull('streams_streams.id')->orWhereNull('streams_fields.id')->delete();
     $translations = $this->model->getTranslationModel();
     $translations->leftJoin('streams_assignments', 'streams_assignments_translations.assignment_id', '=', 'streams_assignments.id')->whereNull('streams_assignments.id')->delete();
 }
 /**
  * Make a Stream instance from the provided compile data.
  *
  * @param  array           $data
  * @return StreamInterface
  */
 public function make(array $data)
 {
     $payload = $data;
     if ($stream = self::$store->get($data)) {
         return $stream;
     }
     $assignments = [];
     $streamModel = new StreamModel();
     $streamTranslations = new EloquentCollection();
     $data['config'] = serialize(array_get($data, 'config', []));
     if ($translations = array_pull($data, 'translations')) {
         foreach ($translations as $attributes) {
             $translation = new StreamModelTranslation();
             $translation->setRawAttributes($attributes);
             $streamTranslations->push($translation);
         }
     }
     $streamModel->setRawAttributes($data);
     $streamModel->setRelation('translations', $streamTranslations);
     unset($this->translations);
     if (array_key_exists('assignments', $data)) {
         foreach ($data['assignments'] as $assignment) {
             if (isset($assignment['field'])) {
                 $assignment['field']['config'] = unserialize($assignment['field']['config']);
                 $fieldModel = new FieldModel();
                 $fieldTranslations = new EloquentCollection();
                 if (isset($assignment['field']['translations'])) {
                     foreach (array_pull($assignment['field'], 'translations') as $attributes) {
                         $translation = new FieldModelTranslation();
                         $translation->setRawAttributes($attributes);
                         $fieldTranslations->push($translation);
                     }
                 }
                 $assignment['field']['config'] = serialize($assignment['field']['config']);
                 $fieldModel->setRawAttributes($assignment['field']);
                 $fieldModel->setRelation('translations', $fieldTranslations);
                 unset($assignment['field']);
                 $assignmentModel = new AssignmentModel();
                 $assignmentTranslations = new EloquentCollection();
                 if (isset($assignment['translations'])) {
                     foreach (array_pull($assignment, 'translations') as $attributes) {
                         $translation = new AssignmentModelTranslation();
                         $translation->setRawAttributes($attributes);
                         $assignmentTranslations->push($translation);
                     }
                 }
                 $assignmentModel->setRawAttributes($assignment);
                 $assignmentModel->setRawAttributes($assignment);
                 $assignmentModel->setRelation('field', $fieldModel);
                 $assignmentModel->setRelation('stream', $streamModel);
                 $assignmentModel->setRelation('translations', $assignmentTranslations);
                 $assignments[] = $assignmentModel;
             }
         }
     }
     $assignmentsCollection = new AssignmentCollection($assignments);
     $streamModel->setRelation('assignments', $assignmentsCollection);
     $streamModel->assignments = $assignmentsCollection;
     self::$store->put($payload, $streamModel);
     return $streamModel;
 }
 /**
  * Return the module name.
  *
  * @param  Module $module
  * @return string
  */
 public function parse(AssignmentModel $assignment)
 {
     return $assignment->getFieldSlug();
 }
 /**
  * Boot the service provider.
  */
 public function boot(Dispatcher $events)
 {
     $events->fire(new Booting());
     // First load our app environment.
     $this->dispatch(new LoadEnvironmentOverrides());
     // Next take care of core utilities.
     $this->dispatch(new SetCoreConnection());
     $this->dispatch(new ConfigureCommandBus());
     $this->dispatch(new ConfigureUriValidator());
     $this->dispatch(new InitializeApplication());
     // Setup and preparing utilities.
     $this->dispatch(new LoadStreamsConfiguration());
     $this->dispatch(new AutoloadEntryModels());
     $this->dispatch(new AddAssetNamespaces());
     $this->dispatch(new AddImageNamespaces());
     $this->dispatch(new AddViewNamespaces());
     // Observe our base models.
     EntryModel::observe(EntryObserver::class);
     FieldModel::observe(FieldObserver::class);
     StreamModel::observe(StreamObserver::class);
     EloquentModel::observe(EloquentObserver::class);
     AssignmentModel::observe(AssignmentObserver::class);
     $this->app->booted(function () use($events) {
         $events->fire(new Booted());
         /* @var AddonManager $manager */
         $manager = $this->app->make('Anomaly\\Streams\\Platform\\Addon\\AddonManager');
         /* @var Dispatcher $events */
         $events = $this->app->make('Illuminate\\Contracts\\Events\\Dispatcher');
         $events->listen('Anomaly\\Streams\\Platform\\View\\Event\\RegisteringTwigPlugins', function (RegisteringTwigPlugins $event) {
             $twig = $event->getTwig();
             foreach ($this->plugins as $plugin) {
                 if (!$twig->hasExtension($plugin)) {
                     $twig->addExtension($this->app->make($plugin));
                 }
             }
             if (!$twig->hasExtension('markdown')) {
                 $twig->addExtension(new MarkdownExtension(new MichelfMarkdownEngine()));
             }
             $twig->addExtension(new Extension(new CacheStrategy(new CacheAdapter($this->app->make(Repository::class)), new CacheKey())));
         });
         $manager->register();
         $this->dispatch(new IncludeRoutes());
         $events->fire(new Ready());
     });
     $this->app->singleton('mailer', function () {
         $mailer = new Mailer($this->app['view'], $this->app['swift.mailer'], $this->app['events']);
         $mailer->setContainer($this->app);
         if ($this->app->bound('Psr\\Log\\LoggerInterface')) {
             $mailer->setLogger($this->app->make('Psr\\Log\\LoggerInterface'));
         }
         if ($this->app->bound('queue')) {
             $mailer->setQueue($this->app['queue.connection']);
         }
         $from = $this->app['config']['mail.from'];
         if (is_array($from) && isset($from['address'])) {
             $mailer->alwaysFrom($from['address'], $from['name']);
         }
         $to = $this->app['config']['mail.to'];
         if (is_array($to) && isset($to['address'])) {
             $mailer->alwaysTo($to['address'], $to['name']);
         }
         $pretend = $this->app['config']->get('mail.pretend', false);
         $mailer->pretend($pretend);
         return $mailer;
     });
     $this->app->singleton('Illuminate\\Mail\\Mailer', function () {
         return $this->app->make('mailer');
     });
     $this->app->singleton('Illuminate\\Contracts\\Mail\\Mailer', function () {
         return $this->app->make('mailer');
     });
     $this->dispatch(new ConfigureTranslator());
 }
 /**
  * Return the module name.
  *
  * @param  Module $module
  * @return string
  */
 public function parse(AssignmentModel $assignment)
 {
     return studly_case($assignment->getFieldSlug());
 }
 /**
  * Boot the service provider.
  */
 public function boot(Dispatcher $events)
 {
     $events->fire(new Booting());
     // First load our app environment.
     $this->dispatch(new LoadEnvironmentOverrides());
     // Next take care of core utilities.
     $this->dispatch(new SetCoreConnection());
     $this->dispatch(new ConfigureUriValidator());
     $this->dispatch(new InitializeApplication());
     // Setup and preparing utilities.
     $this->dispatch(new LoadStreamsConfiguration());
     $this->dispatch(new ConfigureTranslator());
     $this->dispatch(new AutoloadEntryModels());
     $this->dispatch(new AddAssetNamespaces());
     $this->dispatch(new AddImageNamespaces());
     $this->dispatch(new AddViewNamespaces());
     $this->dispatch(new ConfigureScout());
     // Observe our base models.
     EntryModel::observe(EntryObserver::class);
     FieldModel::observe(FieldObserver::class);
     StreamModel::observe(StreamObserver::class);
     EloquentModel::observe(EloquentObserver::class);
     AssignmentModel::observe(AssignmentObserver::class);
     $this->app->booted(function () use($events) {
         $events->fire(new Booted());
         /* @var AddonManager $manager */
         $manager = $this->app->make('Anomaly\\Streams\\Platform\\Addon\\AddonManager');
         /* @var Dispatcher $events */
         $events = $this->app->make('Illuminate\\Contracts\\Events\\Dispatcher');
         $events->listen('Anomaly\\Streams\\Platform\\View\\Event\\RegisteringTwigPlugins', function (RegisteringTwigPlugins $event) {
             $twig = $event->getTwig();
             foreach ($this->plugins as $plugin) {
                 if (!$twig->hasExtension($plugin)) {
                     $twig->addExtension($this->app->make($plugin));
                 }
             }
             if (!$twig->hasExtension('markdown')) {
                 $twig->addExtension(new MarkdownExtension(new MichelfMarkdownEngine()));
             }
             $twig->addExtension(new Extension(new CacheStrategy(new CacheAdapter($this->app->make(Repository::class)), new CacheKey())));
         });
         $manager->register();
         /*
          * Do this after addons are registered
          * so that they can override named routes.
          */
         $this->dispatch(new IncludeRoutes());
         $events->fire(new Ready());
     });
 }