/** * Attempts to extract possible theme slugs from the relative * path given, which may be used to resolve those theme * instances on the theme bag lazily. * * @param string $path * @return array $slugs * @throws \RuntimeException */ public function extractSlugsFromPath($path) { $slugs = array(); $areaDepth = $this->themeBag->getAreaDepth(); $maxThemeDepth = $this->themeBag->getMaxThemeDepth(); $path = trim($path, '/'); $pathParts = explode('/', $path); $pathCount = count($pathParts); // If the path count is less than or equal to the area depth, // the path cannot be in an area as there will be no parts // left for the slug. if ($pathCount <= $areaDepth) { // If the path count is greater than the maximum theme depth // but less than the area depth, then it can't match an area // or a theme. if ($pathCount > $maxThemeDepth) { throw new RuntimeException("Path [{$path}] is too small for area depth [{$areaDepth}] and too large for theme depth [{$maxThemeDepth}] with depth of [{$pathCount}]."); } $slugs[] = $path; } else { // Slice up the array according to the area depth $areaPathParts = array_slice($pathParts, 0, $areaDepth); $themePathParts = array_slice($pathParts, $areaDepth); $themePathPartsCount = count($themePathParts); // If the path count is greater than the maximum theme depth // but less than the area depth, then it can't match an area // or a theme. if ($themePathPartsCount > $maxThemeDepth) { throw new RuntimeException(sprintf('Path [%s] resolved to area [%s] is too large for theme depth [%s] with depth of [%s].', $path, implode('/', $areaPathParts), $maxThemeDepth, $themePathPartsCount)); } // Firstly, we will provide a slug with the area prepended // to follow our convention of area-first before non-area themes. $slugs[] = sprintf('%s::%s', implode('/', $areaPathParts), implode('/', $themePathParts)); // Now, we will check if the path parts are within the range of // the maximum theme depth. If they are, we'll also add that slug // to the array of slugs. If not, we won't throw an Exception as the // developer has already provided a valid slug above. Think of this // as a bonus. if ($pathCount <= $maxThemeDepth) { $slugs[] = $path; } } return $slugs; }