"""Offline ASP.NET semantic verification lab for Chapter 15 (webpages-php)."""
import json
import re
VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"
def case_php_tag_whitespace_check():
broken = "<!DOCTYPE html><html><body><?phpphpinfo();?></body></html>"
fixed = "<!DOCTYPE html><html><body><?php phpinfo(); ?></body></html>"
valid_pattern = re.compile(r"<\?php\s+phpinfo\(\);\s*\?>")
expected = {"broken_valid": False, "fixed_valid": True}
actual = {
"broken_valid": bool(valid_pattern.search(broken)),
"fixed_valid": bool(valid_pattern.search(fixed)),
}
return {
"name": "php_tag_whitespace_check",
"summary": "校验 PHP 起始标签 <?php 后必须留有空白字符,修正原示例 <?phpphpinfo();?> 粘连笔误",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_webmatrix_php_site_creation_flow():
site_cfg = {
"site_name": "Demo_PHP",
"enable_php": True,
"entry_file": "index.php",
"coexists_with": "Demo (CSHTML)",
}
expected = {
"site_name": "Demo_PHP",
"enable_php": True,
"entry_ext": ".php",
}
actual = {
"site_name": site_cfg["site_name"],
"enable_php": site_cfg["enable_php"],
"entry_ext": "." + site_cfg["entry_file"].rsplit(".", 1)[1],
}
return {
"name": "webmatrix_php_site_creation_flow",
"summary": "核对 WebMatrix 创建 Demo_PHP 空站点、勾选 Enable PHP 并新建 index.php 的历史步骤",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_php_vs_razor_syntax_equivalence():
# Render identical HTML greeting from PHP-style vs Razor-style expression
name = "WebDev"
php_rendered = f"<!DOCTYPE html><html><body><p>Hello, {name}</p></body></html>"
razor_rendered = f"<!DOCTYPE html><html><body><p>Hello, {name}</p></body></html>"
expected = {
"outputs_identical": True,
"php_delimiter": "<?php ... ?>",
"razor_delimiter": "@ / @{ ... }",
}
actual = {
"outputs_identical": php_rendered == razor_rendered,
"php_delimiter": "<?php ... ?>",
"razor_delimiter": "@ / @{ ... }",
}
return {
"name": "php_vs_razor_syntax_equivalence",
"summary": "对比 PHP 与 ASP.NET Razor 服务端嵌入模板语法并验证两者最终输出相同的标准 HTML",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_phpinfo_production_security_note():
dev_allowed = True
prod_allowed = False
modern_stack = {"php_runtime": "PHP 8.x + FastCGI / PHP-FPM", "dotnet_runtime": "ASP.NET Core + Kestrel"}
expected = {"expose_phpinfo_in_prod": False, "modern_stacks_count": 2}
actual = {"expose_phpinfo_in_prod": prod_allowed and dev_allowed, "modern_stacks_count": len(modern_stack)}
return {
"name": "phpinfo_production_security_note",
"summary": "验证生产环境禁止公开 phpinfo() 敏感配置信息,并核对现代 PHP 与 ASP.NET Core 独立部署方式",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_php_tag_whitespace_check(),
case_webmatrix_php_site_creation_flow(),
case_php_vs_razor_syntax_equivalence(),
case_phpinfo_production_security_note(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。
入口文件:projects/mvc-webpages-php-lab/explore-webpages-php.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-webpages-php.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2900.zip
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2870.zip