/** * Execute the command. * * @param InputInterface $input * @param OutputInterface $output * * @return void */ public function execute(InputInterface $input, OutputInterface $output) { $baseDir = $input->getArgument('base'); if ($baseDir === null) { $baseDir = $this->_projectBase; } $baseDir = rtrim($baseDir, '/') . '/'; $extensions = AssetResponse::getExtensions(); $pattern = '*.' . implode(',*.', $extensions); $fileList = $this->globRecursive($baseDir . '{' . $pattern . '}', GLOB_BRACE); $hashMap = []; $data = ''; foreach ($fileList as $file) { $key = str_replace($baseDir, '', $file); $hash = ResourceGenerator::getFileHash($file); $hashMap[$key] = $hash; $data .= "{$key} = {$hash}\n"; } $outputFile = $input->getOption('output'); $filename = Path::build($baseDir, $outputFile); if (!file_exists($filename) || is_writable($filename)) { file_put_contents($filename, $data); $output->writeln("Written " . count($fileList) . " file hash keys to " . $filename); } else { $output->writeln("Failed writing to {$filename}"); } }
public function testGetSet() { $view = $this->getMockForAbstractClass('\\Cubex\\View\\ViewModel'); /** * @var $view \Cubex\View\ViewModel */ $view->setTemplateDir('randomDir'); $this->assertEquals('randomDir', $view->getTemplateDir()); $view->setTemplateFile('randomFile'); $this->assertEquals('randomFile', $view->getTemplateFile()); $this->assertEquals(Path::build('randomDir', 'randomFile.phtml'), $view->getTemplatePath('.phtml')); }
public function testConfigurations() { $connectionConfig = new \Packaged\Config\Provider\Ini\IniConfigProvider(Path::build(__DIR__, 'resources', 'connections.ini')); $datastoreConfig = new \Packaged\Config\Provider\Ini\IniConfigProvider(Path::build(__DIR__, 'resources', 'datastores.ini')); $resolver = new \Packaged\Dal\DalResolver($connectionConfig, $datastoreConfig); $this->assertFalse($resolver->hasConnection('conX')); $this->assertTrue($resolver->hasConnection('con1')); $this->assertFalse($resolver->hasDatastore('ds2')); $this->assertFalse($resolver->hasDatastore('ds1')); $this->assertTrue($resolver->hasDatastore('qlds')); $this->assertTrue($resolver->hasDatastore('filesystem')); $connection = $resolver->getConnection('con1'); /** * @var $connection ConfigurableConnection */ $this->assertEquals('Connection Test', $connection->getConfig()->getItem('name')); $this->setExpectedException('\\Packaged\\Dal\\Exceptions\\DalResolver\\ConnectionNotFoundException'); $resolver->getConnection('con2'); }
protected function setUp() { $dbgz = 'http://geolite.maxmind.com/download/' . 'geoip/database/GeoLite2-City.mmdb.gz'; $filename = Path::build(sys_get_temp_dir(), 'GeoLite2-City.mmdb.gz'); $this->_geoipdb = substr($filename, 0, -3); if (!file_exists($this->_geoipdb)) { $opts = ['http' => ['method' => "GET", 'header' => "Accept-language: en\r\n" . "User-Agent: CURL (Cubex Framework; en-us)\r\n"]]; $context = stream_context_create($opts); file_put_contents($filename, file_get_contents($dbgz, false, $context)); $file = gzopen($filename, 'rb'); $out = fopen($this->_geoipdb, 'wb'); while (!gzeof($file)) { fwrite($out, gzread($file, 4096)); } fclose($out); gzclose($file); unlink($filename); } }
/** * Create a temporary filename * * @param $key * * @return string */ private function _createFileName($key) { return Path::build(sys_get_temp_dir(), 'Fortifi-API-Token-' . $key); }
/** * Create the response for the given path * * @param $path * @param Request $request * * @return Response */ public function getResponseForPath($path, Request $request) { if (empty($path)) { //What resources do you expect to find with no path? return $this->invalidUrlResponse(); } $pathInfo = pathinfo($path); //Every dispatch request needs an extension if (empty($pathInfo['extension'])) { return $this->invalidUrlResponse(); } $response = new AssetResponse(); //Grab the correct asset for the requesting extension $asset = $response->assetByExtension($pathInfo['extension']); //Load the options $options = ValueAs::arr($this->_config->getItem($pathInfo['extension'] . '_config'), null); if ($options !== null) { $asset->setOptions($options); } //Lookup the full path on the filesystem $dirMapper = new DirectoryMapper($this->_baseDirectory, $this->_config); $directory = $dirMapper->urlToPath($pathInfo['dirname']); $filePath = Path::build($directory, $pathInfo['basename']); //Do not minify files ending in .min.ext if (substr($pathInfo['filename'], -4) == '.min') { $asset->setOption('minify', false); } //If the asset does not exist on disk, return a not found error if ($directory === null || !file_exists($filePath)) { return $this->notFoundResponse($path); } //Give the asset its file content $asset->setContent(file_get_contents($filePath)); if ($asset instanceof IDispatchableAsset) { //Set the asset manager $asset->setWorkingDirectory(realpath($directory)); $asset->setAssetManager(AssetManager::buildFromUri($path)); } //Create and return the response return $response->createResponse($asset, $request); }
public function testExceptionMissingFile() { $this->setExpectedException('\\Packaged\\Dal\\Exceptions\\DataStore\\DaoNotFoundException'); $file = new FileSystemDao(Path::build(dirname(dirname(__DIR__)), 'missing.file')); $file->load(); }
/** * Generate the URI for the provided details * * @param $type * @param $lookup * @param $domain * @param $path * @param $file * * @return null|string */ public function generateUriPath($type, $lookup, $domain, $path, $file) { $parts = []; //Include the map type $parts[] = $type; //When lookup parts are avilable, e.g. vendor/package, include them if (is_array($lookup)) { foreach ($lookup as $lookupPart) { $parts[] = $lookupPart; } } $parts[] = static::hashDomain($domain); //If not path is available, assume you are requesting the base path if (empty($path)) { $partHash = 'b'; } else { //Build the hashable path $partHash = $this->_mapper->hashDirectoryArray(ValueAs::arr(explode('/', $path))); } $parts[] = $partHash; $baseDir = $this->getBasePath($this->_mapper, $type, (array) $lookup); $filePath = Path::build($baseDir, $path, $file); $fileHash = $this->_dispatcher->getFileHash($filePath); if ($fileHash === null) { //File hash doesnt exist in the cache, so lets look it up $fullPath = Path::build($this->_dispatcher->getBaseDirectory(), $filePath); $fileHash = ResourceGenerator::getFileHash($fullPath); if (!$fileHash) { //If we cant get a hash of the file, its unlikely it exists return null; } //Cache the entry, to optimise should the same resource be re-requested $this->_dispatcher->addFileHashEntry($filePath, $fileHash); } $parts[] = substr($fileHash, 0, 7); //Include the file extension $parts[] = $file; return implode('/', $parts); }
protected function ownFile() { return dirname(__DIR__) . DIRECTORY_SEPARATOR . Path::build('vendor', 'packaged', 'dispatch', 'src', 'AssetManager.php'); }
/** * Go on the filesystem search for the matching directory * * @param $base * @param $hash * * @return null */ public function findPathFromHash($base, $hash) { //If the requesting path is the base, return the base if ($hash === 'b') { return $base; } //Attempt to load the resource from disk if (isset(static::$_pathCache[func_get_arg(0) . $hash])) { return static::$_pathCache[func_get_arg(0) . $hash]; } $hashParts = explode(';', $hash); foreach ($hashParts as $part) { //Search for directories matching the hash $dirs = glob(Path::build($base, substr($part, 0, 2) . '*'), GLOB_ONLYDIR); if (!$dirs) { return null; } //If we only found one folder, lets assume its correct if (!isset($dirs[1])) { $base = $dirs[0]; continue; } //Marker to ensure a valid directory has been located $found = false; //Loop over the directories to match the path foreach ($dirs as $path) { $folder = [substr($path, strlen($base) + 1)]; if ($part == $this->hashDirectoryArray($folder, strlen($part) - 2)) { $base = $path; $found = true; break; } } //The directory could not be found, so just give up if (!$found) { return null; } } //Cache the path to stop future lookups on disk $this->cachePath(func_get_arg(0), $hash, $base); //Path finally matched return $base; }
protected function _getResourceLocation($filename) { return Path::build(dirname(__DIR__), 'resources', 'FileSystem', $filename); }
use Packaged\Helpers\Strings; $output = '<?php namespace Fortifi\\FortifiApi\\ActivityFeed\\Enums; class ActivityFeedStoryEnum { public static $storyTypes = [ '; $stories = Path::globRecursive(Path::build(__DIR__, 'Stories')); foreach ($stories as $story) { $story = str_replace(__DIR__ . '/Stories/', '', $story); //Skip Abstracts and Non Story Classes if (Strings::startsWithAny($story, ['Abstract']) || !Strings::endsWith($story, 'Story.php')) { continue; } $part = explode('/', $story); $storyClass = array_pop($part); $storyClass = str_replace('Story.php', '', $storyClass); $storyNs = 'Fortifi\\FortifiApi\\ActivityFeed\\Stories\\' . implode('\\', $part); $key = Strings::stringToUnderScore($storyClass); //Skip Interfaces if (substr($key, 0, 2) == 'i_') { continue; } $output .= " '{$key}' => '" . $storyNs . "\\" . $storyClass . "Story',\n"; } $output .= ' ]; } '; file_put_contents(Path::build(__DIR__, 'Enums', 'ActivityFeedStoryEnum.php'), $output);
/** * @param Request $request * @param int $type * @param bool $catch * * @return CubexResponse|BinaryFileResponse|Response * @throws \Exception */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { //If the favicon has not been picked up within the public folder //return the cubex favicon if ($request->getRequestUri() === '/favicon.ico') { $favIconPaths = []; $favIconPaths[] = Path::build($this->getProjectRoot(), 'favicon.ico'); $favIconPaths[] = Path::build($this->getProjectRoot(), 'assets', 'favicon.ico'); $favIconPaths[] = Path::build(dirname(__DIR__), 'favicon.ico'); $favPath = null; foreach ($favIconPaths as $favPath) { if (file_exists($favPath)) { break; } } $favicon = new BinaryFileResponse($favPath); $favicon->prepare($request); return $favicon; } try { //Ensure all constants have been configured if ($this->getDocRoot() === null) { throw new \RuntimeException("Cubex has been constructed without a document root provided" . ", you must call createConstants before calling handle."); } //Ensure we are working with a Cubex Request for added functionality if (!$request instanceof CubexRequest) { throw new \InvalidArgumentException('You must use a \\Cubex\\Http\\Request'); } $this->instance('request', $request); //Boot Cubex $this->boot(); //Retrieve the $kernel = $this->makeWithCubex('\\Cubex\\Kernel\\CubexKernel'); if ($kernel instanceof CubexKernel) { $response = $kernel->handle($request, $type, $catch); if (!$response instanceof Response) { throw CubexException::debugException("A valid response was not generated by the default kernel", 500, $response); } return $response; } throw new \RuntimeException("No Cubex Kernel has been configured"); } catch (\Exception $e) { if ($catch) { return $this->exceptionResponse($e); } else { throw $e; } } }
public function testBuildPath() { $this->assertEquals("a" . DIRECTORY_SEPARATOR . "b", Path::build("a", "b")); $this->assertEquals("a" . DIRECTORY_SEPARATOR . "b", Path::build("a", "b")); }
/** * Get the full path to the template file for this viewmodel * * @param string $extension * * @return string */ public function getTemplatePath($extension = '.phtml') { return Path::build($this->getTemplateDir(), $this->getTemplateFile()) . $extension; }