Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions modules/cms/classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Winter\Storm\Exception\ValidationException;
use Winter\Storm\Parse\Bracket as TextParser;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Blade;

/**
* The CMS controller class.
Expand Down Expand Up @@ -1065,9 +1066,14 @@ public function renderPartial($name, $parameters = [], $throwException = true)
* Render the partial
*/
CmsException::mask($partial, 400);
$this->getLoader()->setObject($partial);
$template = $this->getTwig()->load($partial->getFilePath());
$partialContent = $template->render(array_merge($parameters, $this->vars));
if (str_ends_with($partial->fileName, '.blade.php')) {
$partialContent = Blade::render($partial->content, $this->vars);
}
else {
$this->getLoader()->setObject($partial);
$template = $this->getTwig()->load($partial->getFilePath());
$partialContent = $template->render(array_merge($parameters, $this->vars));
}
Comment on lines +1069 to +1076
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Blade rendering path missing component variables.

The Blade rendering at line 1070 only passes $this->vars, while the Twig rendering at line 1075 passes array_merge($parameters, $this->vars). This inconsistency is problematic because:

  • Lines 1022-1063 add component objects to the $parameters array (see line 1045: $parameters[$alias] = ... = $componentObj)
  • These components are only included in the Twig rendering but not in the Blade rendering
  • Blade partials with components won't have access to those component instances
🔎 Proposed fix
 if (str_ends_with($partial->fileName, '.blade.php')) {
-    $partialContent = Blade::render($partial->content, $this->vars);
+    $partialContent = Blade::render($partial->content, array_merge($parameters, $this->vars));
 }
 else {
     $this->getLoader()->setObject($partial);
     $template = $this->getTwig()->load($partial->getFilePath());
     $partialContent = $template->render(array_merge($parameters, $this->vars));
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (str_ends_with($partial->fileName, '.blade.php')) {
$partialContent = Blade::render($partial->content, $this->vars);
}
else {
$this->getLoader()->setObject($partial);
$template = $this->getTwig()->load($partial->getFilePath());
$partialContent = $template->render(array_merge($parameters, $this->vars));
}
if (str_ends_with($partial->fileName, '.blade.php')) {
$partialContent = Blade::render($partial->content, array_merge($parameters, $this->vars));
}
else {
$this->getLoader()->setObject($partial);
$template = $this->getTwig()->load($partial->getFilePath());
$partialContent = $template->render(array_merge($parameters, $this->vars));
}
🤖 Prompt for AI Agents
In modules/cms/classes/Controller.php around lines 1069-1076, the Blade branch
renders with only $this->vars while the Twig branch renders
array_merge($parameters, $this->vars); update the Blade rendering to pass the
same merged variables so component objects added to $parameters are available to
Blade partials (i.e. call Blade::render with array_merge($parameters,
$this->vars) instead of $this->vars).

CmsException::unmask();

if ($partial instanceof Partial) {
Expand Down
27 changes: 27 additions & 0 deletions modules/cms/classes/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Partial extends CmsCompoundObject
*/
protected $dirName = 'partials';

protected $allowedExtensions = ['htm', 'blade'];

/**
* Returns name of a PHP class to us a parent for the PHP class created for the object's PHP section.
* @return string Returns the class name.
Expand All @@ -21,4 +23,29 @@ public function getCodeClassParent()
{
return PartialCode::class;
}

/**
* Returns the base file name and extension. Applies a default extension, if none found.
*/
public function getFileNameParts($fileName = null)
{
if ($fileName === null) {
$fileName = $this->fileName;
}

if (!strlen($extension = pathinfo($fileName, PATHINFO_EXTENSION))) {
$extension = $this->defaultExtension;
$baseFile = $fileName;
}
elseif (($extension = pathinfo($fileName, PATHINFO_EXTENSION)) === 'blade') {
$extension = 'php';
$baseFile = $fileName;
}
else {
$pos = strrpos($fileName, '.');
$baseFile = substr($fileName, 0, $pos);
}

return [$baseFile, $extension];
}
Comment on lines +27 to +50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify getFileNameParts usage, CMS loading system, and Blade partial handling

# 1. Search for usage of getFileNameParts
echo "=== Searching for getFileNameParts usage ==="
rg -n "getFileNameParts" --type=php

# 2. Search for file loading logic
echo -e "\n=== Searching for load() method in CMS classes ==="
rg -n -A 3 "function load\(" --type=php modules/cms/classes/ 2>/dev/null || echo "No matches found or directory not accessible"

# 3. Search for Partial class definition and its inheritance
echo -e "\n=== Searching for Partial class definition ==="
rg -n "class Partial" --type=php -A 5

# 4. Check for .blade files or .blade.php files in themes
echo -e "\n=== Searching for blade template files ==="
find . -name "*blade.php" -o -name "*.blade" 2>/dev/null | head -20 || echo "No blade files found"

Repository: wintercms/winter

Length of output: 4032


🏁 Script executed:

#!/bin/bash
# Read the Partial.php file to understand the code context
echo "=== Reading modules/cms/classes/Partial.php (lines 25-55) ==="
if [ -f "modules/cms/classes/Partial.php" ]; then
  sed -n '25,55p' modules/cms/classes/Partial.php | cat -n
else
  echo "File not found"
fi

Repository: wintercms/winter

Length of output: 1078


Remove redundant pathinfo() call.

The code has a minor inefficiency: line 40 calls pathinfo($fileName, PATHINFO_EXTENSION) again, even though it was already evaluated in the outer if condition (line 36). Cache the result to avoid the redundant call:

-    if (!strlen($extension = pathinfo($fileName, PATHINFO_EXTENSION))) {
+    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
+    
+    if (!strlen($extension)) {
         $extension = $this->defaultExtension;
         $baseFile = $fileName;
     }
-    elseif (($extension = pathinfo($fileName, PATHINFO_EXTENSION)) === 'blade') {
+    elseif ($extension === 'blade') {
         $extension = 'php';
         $baseFile = $fileName;
     }
🤖 Prompt for AI Agents
In modules/cms/classes/Partial.php around lines 27 to 50, the code redundantly
calls pathinfo($fileName, PATHINFO_EXTENSION) a second time in the elseif; reuse
the $extension variable already obtained instead. Replace the second pathinfo()
call with a comparison against the cached $extension (e.g. if ($extension ===
'blade') ...) and keep the same logic for setting $extension and $baseFile in
each branch so $baseFile is defined for all cases.

}
14 changes: 12 additions & 2 deletions modules/system/traits/ViewMaker.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php namespace System\Traits;

use Block;
use Config;
use File;
use Illuminate\Support\Facades\Blade;
use Lang;
use Block;
use SystemException;
use Throwable;
use Config;

/**
* View Maker Trait
Expand Down Expand Up @@ -208,6 +209,10 @@ public function getViewPath(string $fileName, string|array|null $viewPaths = nul
// Check the path for an extension
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if (!empty($ext)) {
if ($ext === 'blade') {
$ext = 'php';
$fileName .= ".php";
}
if (!in_array($ext, $allowedExtensions)) {
throw new SystemException("$ext is not a valid View extension");
}
Expand Down Expand Up @@ -264,6 +269,11 @@ public function makeFileContents(string $filePath, array $extraParams = []): str

$vars = array_merge($this->vars, $extraParams);

if (str_ends_with($filePath, '.blade.php')) {
$fileContent = file_get_contents($filePath);
return Blade::render($fileContent, $vars);
}

$obLevel = ob_get_level();

ob_start();
Expand Down
Loading