/** * Compile an asset collection. * * @param Basset\Collection $collection * @return void */ protected function compile(Collection $collection) { $force = isset($_SERVER['CLI']['FORCE']); // If the compile path does not exist attempt to create it. if (!File::exists($this->compilePath)) { File::mkdir($this->compilePath); } $groups = $collection->getAssets(); if (empty($groups)) { echo "The collection '{$collection->getName()}' has no assets to compile.\n"; } foreach ($groups as $group => $assets) { $path = $this->compilePath . '/' . $collection->getCompiledName($group); // We only compile a collection if a compiled file doesn't exist yet or if a change to one of the assets // in the collection is detected by comparing the last modified times. if (File::exists($path) and File::modified($path) >= $collection->lastModified($group)) { // If the force flag has been set then we'll recompile, otherwise this collection does not need // to be changed. if (!$force) { echo "The {$group}s for the collection '{$collection->getName()}' do not need to be compiled.\n"; continue; } } $compiled = $collection->compile($group); echo "Successfully compiled {$collection->getCompiledName($group)}\n"; File::put($path, $compiled); } }
/** * Prepares the response before sending to the browser. * * @return Laravel\Response */ public function prepare() { $collection = new Collection(null); $asset = $this->getAssetFromUri(URI::current()); if (!($asset = $collection->add($asset))) { return; } // Create a new LaravelResponse object with the contents of the asset. Once we have the // response object we can adjust the headers before sending it to the browser. $this->response = new LaravelResponse($collection->compile($asset->getGroup())); switch ($asset->getGroup()) { case 'style': $this->response->header('content-type', 'text/css'); break; case 'script': $this->response->header('content-type', 'application/javascript'); break; } return $this; }
/** * Build a development collection. * * @param \Basset\Collection $collection * @param string $group * @return void * @throws \Basset\Exceptions\BuildNotRequiredException */ public function buildAsDevelopment(Collection $collection, $group) { // Get the assets of the given group from the collection. The collection is also responsible // for handling any ordering of the assets so that we just need to build them. $assets = $collection->getAssetsWithoutRaw($group); $entry = $this->manifest->make($identifier = $collection->getIdentifier()); // If the collection definition has changed when compared to the manifest entry or if the // collection is being forcefully rebuilt then we'll reset the development assets. if ($this->collectionDefinitionHasChanged($assets, $entry, $group) or $this->force) { $entry->resetDevelopmentAssets($group); } else { $assets = $assets->filter(function ($asset) use($entry) { return !$entry->hasDevelopmentAsset($asset) or $asset->getBuildPath() != $entry->getDevelopmentAsset($asset); }); } if (!$assets->isEmpty()) { foreach ($assets as $asset) { $path = "{$this->buildPath}/{$identifier}/{$asset->getBuildPath()}"; // If the build directory does not exist we'll attempt to recursively create it so we can // build the asset to the directory. !$this->files->exists($directory = dirname($path)) and $this->files->makeDirectory($directory, 0777, true); $this->files->put($path, $this->gzip($asset->build())); // Add the development asset to the manifest entry so that we can save the built asset // to the manifest. $entry->addDevelopmentAsset($asset); } } else { throw new BuildNotRequiredException(); } }
/** * Serve a collections raw assets. * * @param \Basset\Collection $collection * @param string $group * @param string $format * @return array */ protected function serveRawAssets(Collection $collection, $group, $format) { $responses = array(); foreach ($collection->getAssetsOnlyRaw($group) as $asset) { $path = $asset->getRelativePath(); $responses[] = $this->{'create' . studly_case($group) . 'Element'}($path, $format); } return $responses; }
/** * Get the collections identifier from a collection instance. * * @param string|\Basset\Collection $collection * @return string */ protected function getCollectionNameFromInstance($collection) { return $collection instanceof Collection ? $collection->getIdentifier() : $collection; }
/** * Clean collection development files. * * @param \Basset\Collection $collection * @param \Basset\Manifest\Entry $entry * @return void */ protected function cleanDevelopmentFiles(Collection $collection, Entry $entry) { foreach ($entry->getDevelopmentAssets() as $assets) { foreach ($assets as $asset) { $wildcardPath = $this->replaceFingerprintWithWildcard($asset); $this->deleteMatchingFiles($this->buildPath . '/' . $collection->getIdentifier() . '/' . $wildcardPath, array_values($assets)); } } }