Skip to content

Commit

Permalink
Wrap extract calls in IIFEs in View (#4113)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan authored Jan 28, 2021
1 parent 66a02fc commit 2d930ca
Showing 1 changed file with 17 additions and 47 deletions.
64 changes: 17 additions & 47 deletions system/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ class View implements RendererInterface
*/
protected $currentSection;

//--------------------------------------------------------------------

/**
* Constructor
*
Expand All @@ -145,8 +143,6 @@ public function __construct(ViewConfig $config, string $viewPath = null, FileLoc
$this->saveData = (bool) $config->saveData;
}

//--------------------------------------------------------------------

/**
* Builds the output based upon a file name and any
* data that has already been set.
Expand Down Expand Up @@ -209,7 +205,6 @@ public function render(string $view, array $options = null, bool $saveData = nul

// Make our view data available to the view.
$this->tempData = $this->tempData ?? $this->data;
extract($this->tempData);

if ($saveData)
{
Expand All @@ -219,10 +214,12 @@ public function render(string $view, array $options = null, bool $saveData = nul
// Save current vars
$renderVars = $this->renderVars;

ob_start();
include $this->renderVars['file']; // PHP will be processed
$output = ob_get_contents();
@ob_end_clean();
$output = (function (): string {
extract($this->tempData);
ob_start();
include $this->renderVars['file'];
return ob_get_clean() ?: '';
})();

// Get back current vars
$this->renderVars = $renderVars;
Expand All @@ -243,7 +240,9 @@ public function render(string $view, array $options = null, bool $saveData = nul

$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']);

if (($this->debug && (! isset($options['debug']) || $options['debug'] === true)) && in_array('CodeIgniter\Filters\DebugToolbar', service('filters')->getFiltersClass()['after'], true))
if (($this->debug && (! isset($options['debug']) || $options['debug'] === true))
&& in_array('CodeIgniter\Filters\DebugToolbar', service('filters')->getFiltersClass()['after'], true)
)
{
$toolbarCollectors = config(Toolbar::class)->collectors;

Expand All @@ -252,7 +251,8 @@ public function render(string $view, array $options = null, bool $saveData = nul
// Clean up our path names to make them a little cleaner
$this->renderVars['file'] = clean_path($this->renderVars['file']);
$this->renderVars['file'] = ++$this->viewsCount . ' ' . $this->renderVars['file'];
$output = '<!-- DEBUG-VIEW START ' . $this->renderVars['file'] . ' -->' . PHP_EOL

$output = '<!-- DEBUG-VIEW START ' . $this->renderVars['file'] . ' -->' . PHP_EOL
. $output . PHP_EOL
. '<!-- DEBUG-VIEW ENDED ' . $this->renderVars['file'] . ' -->' . PHP_EOL;
}
Expand All @@ -269,8 +269,6 @@ public function render(string $view, array $options = null, bool $saveData = nul
return $output;
}

//--------------------------------------------------------------------

/**
* Builds the output based upon a string and any
* data that has already been set.
Expand All @@ -292,28 +290,24 @@ public function renderString(string $view, array $options = null, bool $saveData
$saveData = $saveData ?? $this->saveData;
$this->tempData = $this->tempData ?? $this->data;

extract($this->tempData);

if ($saveData)
{
$this->data = $this->tempData;
}

ob_start();
$incoming = '?>' . $view;
eval($incoming);
$output = ob_get_contents();
@ob_end_clean();
$output = (function (string $view): string {
extract($this->tempData);
ob_start();
eval('?>' . $view);
return ob_get_clean() ?: '';
})($view);

$this->logPerformance($start, microtime(true), $this->excerpt($view));

$this->tempData = null;

return $output;
}

//--------------------------------------------------------------------

/**
* Extract first bit of a long string and add ellipsis
*
Expand All @@ -326,8 +320,6 @@ public function excerpt(string $string, int $length = 20): string
return (strlen($string) > $length) ? substr($string, 0, $length - 3) . '...' : $string;
}

//--------------------------------------------------------------------

/**
* Sets several pieces of view data at once.
*
Expand All @@ -350,8 +342,6 @@ public function setData(array $data = [], string $context = null): RendererInter
return $this;
}

//--------------------------------------------------------------------

/**
* Sets a single piece of view data.
*
Expand All @@ -375,8 +365,6 @@ public function setVar(string $name, $value = null, string $context = null): Ren
return $this;
}

//--------------------------------------------------------------------

/**
* Removes all of the view data from the system.
*
Expand All @@ -389,8 +377,6 @@ public function resetData(): RendererInterface
return $this;
}

//--------------------------------------------------------------------

/**
* Returns the current data that will be displayed in the view.
*
Expand All @@ -401,8 +387,6 @@ public function getData(): array
return $this->tempData ?? $this->data;
}

//--------------------------------------------------------------------

/**
* Specifies that the current view should extend an existing layout.
*
Expand All @@ -415,8 +399,6 @@ public function extend(string $layout)
$this->layout = $layout;
}

//--------------------------------------------------------------------

/**
* Starts holds content for a section within the layout.
*
Expand All @@ -429,8 +411,6 @@ public function section(string $name)
ob_start();
}

//--------------------------------------------------------------------

/**
* @throws RuntimeException
*/
Expand All @@ -453,8 +433,6 @@ public function endSection()
$this->currentSection = null;
}

//--------------------------------------------------------------------

/**
* Renders a section's contents.
*
Expand All @@ -476,8 +454,6 @@ public function renderSection(string $sectionName)
}
}

//--------------------------------------------------------------------

/**
* Used within layout views to include additional views.
*
Expand All @@ -492,8 +468,6 @@ public function include(string $view, array $options = null, $saveData = true):
return $this->render($view, $options, $saveData);
}

//--------------------------------------------------------------------

/**
* Returns the performance data that might have been collected
* during the execution. Used primarily in the Debug Toolbar.
Expand All @@ -505,8 +479,6 @@ public function getPerformanceData(): array
return $this->performanceData;
}

//--------------------------------------------------------------------

/**
* Logs performance data for rendering a view.
*
Expand All @@ -527,6 +499,4 @@ protected function logPerformance(float $start, float $end, string $view)
];
}
}

//--------------------------------------------------------------------
}

0 comments on commit 2d930ca

Please sign in to comment.