/**
* Get the evaluated view contents for the given view.
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\Contracts\View\View
*/
public function make($view, $data = [], $mergeData = [])
{// Get the evaluated view contents for the given view.
if (isset($this->aliases[$view])) {
$view = $this->aliases[$view];
}// if isset this alias view , set it
$view = $this->normalizeName($view);// get the normal name
$path = $this->finder->find($view);// get the view path
$data = array_merge($mergeData, $this->parseData($data));// get the data
$this->callCreator($view = new View($this, $this->getEngineFromPath($path), $view, $path, $data));
// call Creator make it
return $view;// return it, in this way
}
/**
* Normalize a view name.
*
* @param string $name
* @return string
*/
protected function normalizeName($name)
{//normalizeName a view name.
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;// set the delimiter
if (strpos($name, $delimiter) === false) {
return str_replace('/', '.', $name);
} // str replace this default delimiter
list($namespace, $name) = explode($delimiter, $name);// get the namespace and name
return $namespace.$delimiter.str_replace('/', '.', $name);// first it is delimiter
//
}
/**
* Parse the given data into a raw array.
*
* @param mixed $data
* @return array
*/
protected function parseData($data)
{
return $data instanceof Arrayable ? $data->toArray() : $data;
}// parse the given data into a raw array.
// maybe it is a not good type
/**
* Get the evaluated view contents for a named view.
*
* @param string $view
* @param mixed $data
* @return \Illuminate\Contracts\View\View
*/
public function of($view, $data = [])
{
return $this->make($this->names[$view], $data);
}//return make
/**
* Register a named view.
*
* @param string $view
* @param string $name
* @return void
*/
public function name($view, $name)
{
$this->names[$name] = $view;
}// register a named view.
/**
* Add an alias for a view.
*
* @param string $view
* @param string $alias
* @return void
*/
public function alias($view, $alias)
{
$this->aliases[$alias] = $view;
}//alias set it
/**
* Determine if a given view exists.
*
* @param string $view
* @return bool
*/
public function exists($view)
{
try {
$this->finder->find($view);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
}// Determine if a given view exists.
/**
* Get the rendered contents of a partial from a loop.
*
* @param string $view
* @param array $data
* @param string $iterator
* @param string $empty
* @return string
*/
public function renderEach($view, $data, $iterator, $empty = 'raw|')
{// Get the rendered contents of a partial from a loop
$result = '';// set the result store.
// If is actually data in the array, we will loop through the data and append
// an instance of the partial view to the final result HTML passing in the
// iterated value of this data array, allowing the views to access them.
if (count($data) > 0) {
foreach ($data as $key => $value) {
$data = ['key' => $key, $iterator => $value];
$result .= $this->make($view, $data)->render();
}
}// if is actually data in the array. we will loop through the data and append
// an instance of the partial view to the final result HTML passing in the
// iterated value of this data array, allowing the views to access them.
// If there is no data in the array, we will render the contents of the empty
// view. Alternatively, the "empty view" could be a raw string that begins
// with "raw|" for convenience and to let this know that it is a string.
else {
if (Str::startsWith($empty, 'raw|')) {
$result = substr($empty, 4);
} else {
$result = $this->make($empty)->render();
}
}// if there is no data in the array, we will render the contents of the empty
// view. Alternatively, the "empty view" could be a raw string that beings
// with "raw" for convenience and to let this know that it is a string.
return $result;
}// this result
/**
* Get the appropriate view engine for the given path.
*
* @param string $path
* @return \Illuminate\View\Engines\EngineInterface
*
* @throws \InvalidArgumentException
*/
public function getEngineFromPath($path)
{// Get the appropriate view engine for the given path.
if (! $extension = $this->getExtension($path)) {
throw new InvalidArgumentException("Unrecognized extension in file: $path");
}//get the appropriate
$engine = $this->extensions[$extension];// get engine
return $this->engines->resolve($engine);// engines
}
/**
* Get the extension used by the view file.
*
* @param string $path
* @return string
*/
protected function getExtension($path)
{// get Extension
$extensions = array_keys($this->extensions);
// extensions get the array_keys
return Arr::first($extensions, function ($key, $value) use ($path) {
return Str::endsWith($path, $value);
});// Arr::first
}// Get the extension used by the view file.
/**
* Add a piece of shared data to the environment.
*
* @param array|string $key
* @param mixed $value
* @return mixed
*/
public function share($key, $value = null)
{// Add a piece of shared data to the environment
if (! is_array($key)) {// no array
return $this->shared[$key] = $value;
}
foreach ($key as $innerKey => $innerValue) {// loop this innerKey
$this->share($innerKey, $innerValue);
}// loop this share array
}
/**
* Register a view creator event.
*
* @param array|string $views
* @param \Closure|string $callback
* @return array
*/
public function creator($views, $callback)
{// register a view creator event.
$creators = [];// set the creator
foreach ((array) $views as $view) {//loop this views
$creators[] = $this->addViewEvent($view, $callback, 'creating: ');
}// set the creators
return $creators;// creators
}
/**
* Register multiple view composers via an array.
*
* @param array $composers
* @return array
*/
public function composers(array $composers)
{// register multiple view composers via an array.
$registered = [];// registered
foreach ($composers as $callback => $views) {// loop this composers
$registered = array_merge($registered, $this->composer($views, $callback));
}
return $registered;
}//return this registered
// a big wrap function
/**
* Register a view composer event.
*
* @param array|string $views
* @param \Closure|string $callback
* @param int|null $priority
* @return array
*/
public function composer($views, $callback, $priority = null)
{
$composers = [];
foreach ((array) $views as $view) {
$composers[] = $this->addViewEvent($view, $callback, 'composing: ', $priority);
}
return $composers;
}// a composer
/**
* Add an event for a given view.
*
* @param string $view
* @param \Closure|string $callback
* @param string $prefix
* @param int|null $priority
* @return \Closure|null
*/
protected function addViewEvent($view, $callback, $prefix = 'composing: ', $priority = null)
{//Add an event for a given view.
$view = $this->normalizeName($view);// get view
if ($callback instanceof Closure) {// Closure is a system function
$this->addEventListener($prefix.$view, $callback, $priority);
return $callback;
} elseif (is_string($callback)) {
return $this->addClassEvent($view, $callback, $prefix, $priority);
}
}// two type way
/**
* Register a class based view composer.
*
* @param string $view
* @param string $class
* @param string $prefix
* @param int|null $priority
* @return \Closure
*/
protected function addClassEvent($view, $class, $prefix, $priority = null)
{// Register a class based view composer
$name = $prefix.$view;// name prefix.view
// When registering a class based view "composer", we will simply resolve the
// classes from the application IoC container then call the compose method
// on the instance. This allows for convenient, testable view composers.
$callback = $this->buildClassEventCallback($class, $prefix);
// when registering a class based view "composer", we will simply resolve the
// classes from the application IoC container then call the compose method
// on the instance.This allows for convenient, testable view composers.
$this->addEventListener($name, $callback, $priority);// add Event Listener
return $callback;// call back
}
/**
* Add a listener to the event dispatcher.
*
* @param string $name
* @param \Closure $callback
* @param int|null $priority
* @return void
*/
protected function addEventListener($name, $callback, $priority = null)
{//Add a listener to the event dispatcher
if (is_null($priority)) {//null
$this->events->listen($name, $callback);
} else {// listen
$this->events->listen($name, $callback, $priority);
}
}
/**
* Build a class based container callback Closure.
*
* @param string $class
* @param string $prefix
* @return \Closure
*/
protected function buildClassEventCallback($class, $prefix)
{//Build a class based container callback Closure.
list($class, $method) = $this->parseClassEvent($class, $prefix);
// Once we have the class and method name, we can build the Closure to resolve
// the instance out of the IoC container and call the method on it with the
// given arguments that are passed to the Closure as the composer's data.
return function () use ($class, $method) {
$callable = [$this->container->make($class), $method];
return call_user_func_array($callable, func_get_args());
};// Once we have the class and method name, we can build the Closure to resolve
// the instance out of the IoC container and call the method on it with the
// given arguments that are passed to the Closure as the composer's data
}