Files
esun3d/app/code/Esun/Wiki/Setup/Patch/Data/InstallDefaultWikiContent.php
2026-09-15 10:35:56 +08:00

164 lines
7.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Wiki\Setup\Patch\Data;
use Esun\Wiki\Model\CategoryFactory;
use Esun\Wiki\Model\ResourceModel\Category as CategoryResource;
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class InstallDefaultWikiContent implements DataPatchInterface
{
public function __construct(
private readonly ModuleDataSetupInterface $moduleDataSetup,
private readonly CategoryFactory $categoryFactory,
private readonly CategoryResource $categoryResource,
private readonly CollectionFactory $collectionFactory
) {
}
public function apply(): self
{
$this->moduleDataSetup->getConnection()->startSetup();
if ($this->collectionFactory->create()->getSize() > 0) {
$this->moduleDataSetup->getConnection()->endSetup();
return $this;
}
$industryKnowledgeId = $this->createCategory([
'title' => 'Industry Knowledge',
'slug' => 'industry-knowledge',
'summary' => 'eSUN Wiki industry knowledge base.',
'sort_order' => 10,
]);
$productKnowledgeId = $this->createCategory([
'title' => 'Material & eSUN Product Knowledge',
'slug' => 'material-esun-product-knowledge',
'summary' => 'Material guides, product tips, and application knowledge for eSUN users.',
'sort_order' => 20,
]);
$this->createCategory([
'parent_id' => $industryKnowledgeId,
'title' => 'Overview of 3D Printing',
'slug' => 'overview-of-3d-printing',
'summary' => 'Introduction to various 3D printing technologies: filament, resin, powder, metal and more.',
'content' => $this->overviewContent(),
'sort_order' => 10,
]);
$fdmId = $this->createCategory([
'parent_id' => $productKnowledgeId,
'title' => 'FDM Materials for 3D Printing',
'slug' => 'fdm-materials-for-3d-printing',
'summary' => 'Understand PLA, PETG, ABS and other common FDM/FFF 3D printing materials.',
'content' => $this->fdmContent(),
'sort_order' => 20,
]);
foreach (['PLA', 'PETG(PET)', 'ABS', 'ASA', 'TPU', 'PA', 'PP', 'PC', 'PPS', 'PEBA'] as $index => $title) {
$this->createCategory([
'parent_id' => $fdmId,
'title' => $title,
'slug' => $this->slugify($title),
'summary' => sprintf('%s material quick reference.', $title),
'content' => $this->materialContent($title),
'sort_order' => ($index + 1) * 10,
]);
}
$this->moduleDataSetup->getConnection()->endSetup();
return $this;
}
/**
* @param mixed[] $data
*/
private function createCategory(array $data): int
{
$category = $this->categoryFactory->create();
$category->addData(array_merge([
'parent_id' => null,
'summary' => '',
'content' => '',
'is_active' => 1,
'sort_order' => 0,
], $data));
$this->categoryResource->save($category);
return (int) $category->getId();
}
private function overviewContent(): string
{
return <<<'HTML'
<h2>Introduction to Various 3D Printing Technologies</h2>
<p>3D printing covers several process families including FDM/FFF filament printing, resin printing, powder-based printing, and metal additive manufacturing. Each process has its own material range, accuracy, strength profile, and post-processing workflow.</p>
<h2>How to Choose a Process</h2>
<p>For beginners and daily prototyping, FDM printing is often the easiest entry point. Resin printing is useful for fine details, while powder and metal processes are commonly chosen for industrial production scenarios.</p>
HTML;
}
private function fdmContent(): string
{
return <<<'HTML'
<p><a href="#pla">PLA (Polylactic Acid)</a> is the most popular and widely used thermoplastic filament for FDM/FFF 3D printing. Derived from renewable organic resources such as corn starch and sugarcane, it is a biodegradable bioplastic that offers a perfect balance of printability, affordability, and environmental friendliness.</p>
<p>FDM materials differ in toughness, heat resistance, surface finish, flexibility, and engineering performance. Choosing the right filament starts with the application environment and the level of mechanical stress the part will face.</p>
<figure class="esun-wiki-content-figure">
<img src="{{view url='Magento_Theme::images/background-1.png'}}" alt="eSUN material knowledge">
</figure>
<h2 id="pla">PLA</h2>
<p>PLA is easy to print, has low warping, and is suitable for prototypes, educational models, display pieces, and general-purpose fixtures. It is a strong first choice for new users because it prints well on most desktop FDM printers.</p>
<h2>PETG(PET)</h2>
<p>PETG balances strength, toughness, and chemical resistance. It is often used for functional parts that need more durability than PLA while still remaining approachable to print.</p>
<h2>ABS</h2>
<p>ABS offers better heat resistance and impact strength, but it requires a controlled print environment and ventilation to reduce warping and odor during printing.</p>
<h2>ASA</h2>
<p>ASA is selected for outdoor applications because it has improved UV and weather resistance compared with ABS while maintaining similar engineering properties.</p>
<h2>TPU</h2>
<p>TPU is a flexible material for soft-touch parts, protective covers, grips, seals, and elastic components. Print speed and extrusion control are important for stable TPU printing.</p>
<h2>PA</h2>
<p>PA, commonly known as nylon, provides high toughness and wear resistance. It absorbs moisture easily, so drying and storage are essential before printing.</p>
<h2>PP</h2>
<p>PP is lightweight, fatigue-resistant, and chemically resistant. Bed adhesion and warping control are the key challenges when printing PP.</p>
<h2>PC</h2>
<p>PC offers high strength and temperature resistance for demanding functional parts. It usually requires a high-temperature printer setup.</p>
<h2>PPS</h2>
<p>PPS is an advanced material with excellent chemical and thermal resistance, used in specialized industrial scenarios.</p>
<h2>PEBA</h2>
<p>PEBA is a flexible engineering material with elasticity and resilience, often selected where flexibility and low-temperature performance are required.</p>
HTML;
}
private function materialContent(string $title): string
{
return sprintf(
'<h2>%1$s Overview</h2><p>%1$s is part of the eSUN FDM material knowledge base. Add detailed printing settings, application cases, product recommendations, storage guidance, and troubleshooting content from the Magento admin.</p><h2>Printing Tips</h2><p>Keep material dry, verify nozzle and bed temperatures, and tune print speed according to the product data sheet.</p>',
$title
);
}
private function slugify(string $value): string
{
$value = strtolower(trim($value));
$value = (string) preg_replace('/[^a-z0-9]+/', '-', $value);
return trim($value, '-') ?: 'wiki-category';
}
public static function getDependencies(): array
{
return [];
}
public function getAliases(): array
{
return [];
}
}