/** * Get the path to a template with a named path. * * @param string $name * @return string */ protected function findNamedPathView($name) { list($namespace, $view) = $this->getNamespaceSegments($name);// get this namespace and view return $this->findInPaths($view, $this->hints[$namespace]);// findInPaths }// Get the path to a template with a named path /** * Get the segments of a template with a named path. * * @param string $name * @return array * * @throws \InvalidArgumentException */ protected function getNamespaceSegments($name) {// Get the segments of a template with a named path. $segments = explode(static::HINT_PATH_DELIMITER, $name);// get a segments // in this segments if (count($segments) != 2) { throw new InvalidArgumentException("View [$name] has an invalid name."); }// if the result not like as we think, so threw exception if (! isset($this->hints[$segments[0]])) { throw new InvalidArgumentException("No hint path defined for [{$segments[0]}]."); }// if no hints , we will throw Exception return $segments;// return segments } /** * Find the given view in the list of paths. * * @param string $name * @param array $paths * @return string * * @throws \InvalidArgumentException */ protected function findInPaths($name, $paths) {//Find the given view in the list of paths. foreach ((array) $paths as $path) { foreach ($this->getPossibleViewFiles($name) as $file) { if ($this->files->exists($viewPath = $path.'/'.$file)) { return $viewPath;// if find it just return } }// get Possible View Files }// loop path throw new InvalidArgumentException("View [$name] not found.");// default throw Exception } /** * Get an array of possible view files. * * @param string $name * @return array */ protected function getPossibleViewFiles($name) {//get Possible View Files return array_map(function ($extension) use ($name) { return str_replace('.', '/', $name).'.'.$extension; }, $this->extensions);// two powerfull }//Get an array of possible view files. /** * Add a location to the finder. * * @param string $location * @return void */ public function addLocation($location) { $this->paths[] = $location;// add location to path }//Add a location to the finder /** * Add a namespace hint to the finder. * * @param string $namespace * @param string|array $hints * @return void */ public function addNamespace($namespace, $hints) {//add a namespace hint to the finder $hints = (array) $hints;// get the array hints if (isset($this->hints[$namespace])) { $hints = array_merge($this->hints[$namespace], $hints); }// if it is isset, combine it with new hints $this->hints[$namespace] = $hints;// then reset it } /** * Prepend a namespace hint to the finder. * * @param string $namespace * @param string|array $hints * @return void */ public function prependNamespace($namespace, $hints) {//Prepend a namespace hint to the finder. $hints = (array) $hints;// get a right hints if (isset($this->hints[$namespace])) { $hints = array_merge($hints, $this->hints[$namespace]); }// this is a good way to hints $this->hints[$namespace] = $hints;// rewrite it } /** * Register an extension with the view finder. * * @param string $extension * @return void */ public function addExtension($extension) {//register an extension with the view finder. if (($index = array_search($extension, $this->extensions)) !== false) { unset($this->extensions[$index]); }// index array_unshift($this->extensions, $extension); }// add function use this system function to add it /** * Returns whether or not the view specify a hint information. * * @param string $name * @return bool */ public function hasHintInformation($name) { return strpos($name, static::HINT_PATH_DELIMITER) > 0; }// Returns whether or not the view specify a hint information. /** * Get the filesystem instance. * * @return \Illuminate\Filesystem\Filesystem */ public function getFilesystem() { return $this->files;// return files }// get the filesystem instance. /** * Get the active view paths. * * @return array */ public function getPaths() { return $this->paths; }//get paths /** * Get the namespace to file path hints. * * @return array */ public function getHints() { return $this->hints; }//get hints /** * Get registered extensions. * * @return array */ public function getExtensions() { return $this->extensions; }// get extensions }
[李景山php]每天laravel-20161114|FileViewFinder-2.php
原创
©著作权归作者所有:来自51CTO博客作者lijingsan1的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章