/** * Register the service provider. * * @return void */ public function register() { $source_config = __DIR__ . '/../../config/fractal.php'; $this->mergeConfigFrom($source_config, 'fractal'); $this->app->singleton('fractal', function ($app) { // retrieves configurations $autoload = $app['config']->get('fractal.autoload'); $input_key = $app['config']->get('fractal.input_key'); $exclude_key = $app['config']->get('fractal.exclude_key'); $serializer = $app['config']->get('fractal.serializer'); // creating fractal manager instance $manager = new Manager(); $factalNamespace = 'League\\Fractal\\Serializer\\'; $loadSerializer = class_exists($factalNamespace . $serializer) ? $factalNamespace . $serializer : $serializer; $manager->setSerializer(new $loadSerializer()); if ($autoload === true and $includes = $app['request']->input($input_key)) { $manager->parseIncludes($includes); } if ($app['request']->has($exclude_key)) { $manager->parseExcludes($app['request']->input($exclude_key)); } return new FractalServices($manager, $app['app']); }); $this->app->alias('fractal', FractalServices::class); // register our command here $this->app['command.transformer.generate'] = $this->app->share(function ($app) { return new TransformerGeneratorCommand($app['config'], $app['view'], $app['files'], $app); }); $this->commands('command.transformer.generate'); }
/** * excludes sub level from data transformer. * * @param string|array $excludes * * @return $this */ public function excludes($excludes) { if (is_string($excludes)) { $excludes = explode(',', $excludes); } // when autoload is enable, we need to merge user requested includes with the predefined includes. if ($this->autoload and $this->request->get($this->exclude_key)) { $excludes = array_merge($excludes, explode(',', $this->request->get($this->exclude_key))); } $this->manager->parseExcludes($excludes); return $this; }
/** * Create fractal data. * * @return \League\Fractal\Scope * * @throws \Spatie\Fractal\Exceptions\InvalidTransformation * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified */ public function createData() { if (is_null($this->transformer)) { throw new NoTransformerSpecified(); } if (!is_null($this->serializer)) { $this->manager->setSerializer($this->serializer); } if (!is_null($this->includes)) { $this->manager->parseIncludes($this->includes); } if (!is_null($this->excludes)) { $this->manager->parseExcludes($this->excludes); } return $this->manager->createData($this->getResource()); }