/** * Execute the console command. * * @param Search $search * @param IndexManager $manager * @param StreamRepositoryInterface $streams */ public function fire(Search $search, IndexManager $manager, StreamRepositoryInterface $streams, EntryRepositoryInterface $repository) { $stream = $this->argument('stream'); list($namespace, $slug) = explode('.', $stream); if (!($stream = $streams->findBySlugAndNamespace($slug, $namespace))) { $this->error('Stream [' . $this->argument('stream') . '] could not be found.'); return; } /* @var EntryModel $model */ $repository->setModel($model = $stream->getEntryModel()); /** * If the stream is empty we can't * really index it. */ if (!($entry = $repository->first())) { $this->error('Stream [' . $this->argument('stream') . '] is empty.'); } /** * If the stream does not have a valid * search configuration then we don't * know how to insert it's entries. */ if (!($config = $this->dispatch(new GetConfig($entry)))) { $this->error('Stream [' . $this->argument('stream') . '] does not have a search configuration.'); } $this->info('Deleting ' . $this->argument('stream')); $search->search('stream', $stream)->delete(); $this->info('Rebuilding ' . $this->argument('stream')); $this->output->progressStart($repository->count()); foreach ($repository->all() as $entry) { $manager->insert($entry, $config); $this->output->progressAdvance(); } $this->output->progressFinish(); }
/** * Export all entries. * * @param $addon * @param $namespace * @param $stream * @return \Illuminate\Http\RedirectResponse */ public function export($addon, $namespace, $stream) { $addon = $this->addons->get($addon); /* @var StreamInterface $stream */ $stream = $this->streams->findBySlugAndNamespace($stream, $namespace); /* * Resolve the model and set * it on the repository. */ $this->repository->setModel($this->container->make($stream->getEntryModelName())); if (!$this->authorizer->authorize($addon->getNamespace($stream->getSlug() . '.export'))) { abort(403); } $headers = ['Content-Disposition' => 'attachment; filename=' . $stream->getSlug() . '.csv', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Content-type' => 'text/csv', 'Pragma' => 'public', 'Expires' => '0']; $callback = function () { $output = fopen('php://output', 'w'); foreach ($this->repository->all() as $k => $entry) { if ($k == 0) { fputcsv($output, array_keys($entry->toArray())); } fputcsv($output, $entry->toArray()); } fclose($output); }; return $this->response->stream($callback, 200, $headers); }