/** * 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); }
/** * Handle the command. * * @param EntryRepositoryInterface $repository * @param Repository $config * @return string */ public function handle(EntryRepositoryInterface $repository, Repository $config) { $path = $this->fieldType->getStoragePath(); $entry = $this->fieldType->getEntry(); if (!file_exists($this->fieldType->getStoragePath())) { $this->dispatch(new PutFile($this->fieldType)); } $content = $this->dispatch(new GetFile($this->fieldType)); /** * If content is the same then * use the content - doesn't matter. */ if (md5($content) == md5(array_get($entry->getAttributes(), $this->fieldType->getField()))) { return $content; } /** * If the file is newer and we're debugging * then update with the file's content. */ if (filemtime($path) > $entry->lastModified()->timestamp && $config->get('app.debug')) { $repository->save($entry->setRawAttribute($this->fieldType->getField(), $content)); } /** * If the file is newer and we're NOT debugging * then update with the file with the database. */ if (filemtime($path) > $entry->lastModified()->timestamp && !$config->get('app.debug')) { $this->dispatch(new PutFile($this->fieldType)); $content = array_get($entry->getAttributes(), $this->fieldType->getField()); } /** * If the database is newer then update the file * since that is what we use anyways. */ if (filemtime($path) < $entry->lastModified()->timestamp) { $this->dispatch(new PutFile($this->fieldType)); $content = array_get($entry->getAttributes(), $this->fieldType->getField()); } $this->dispatch(new ClearCache()); return $content; }
/** * Handle the command. * * @param EntryRepositoryInterface $repository * @return string */ public function handle(EntryRepositoryInterface $repository) { $path = $this->fieldType->getStoragePath(); $entry = $this->fieldType->getEntry(); if (!file_exists($this->fieldType->getStoragePath())) { $this->dispatch(new PutFile($this->fieldType)); } $content = $this->dispatch(new GetFile($this->fieldType)); if (md5($content) == md5(array_get($entry->getAttributes(), $this->fieldType->getField()))) { return $content; } if (filemtime($path) > $entry->lastModified()->timestamp) { $repository->save($entry->setRawAttribute($this->fieldType->getField(), $content)); } if (filemtime($path) < $entry->lastModified()->timestamp) { $this->dispatch(new PutFile($this->fieldType)); $content = array_get($entry->getAttributes(), $this->fieldType->getField()); } $this->dispatch(new ClearCache()); return $content; }