forked from coreshop/CoreShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Frontend] introduce template installer and new best-practice
- Loading branch information
1 parent
d9586dc
commit 6ff8c4a
Showing
12 changed files
with
308 additions
and
45 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
docs/03_Development/15_Frontend_Bundle/02_Best_Practices.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Best Practices for Frontend in CoreShop | ||
|
||
We learned a lot over the years and want to share our best practices with you. This guide will help you to get the most | ||
out of CoreShop and to avoid common pitfalls. | ||
|
||
## Server Side Rendering | ||
If you do PHP Server Side Rendering with Twig, you should enable the Frontend Bundle in bundles.php: | ||
|
||
```php | ||
// config/bundles.php | ||
return [ | ||
// ... | ||
CoreShop\Bundle\FrontendBundle\CoreShopFrontendBundle::class => ['all' => true], | ||
]; | ||
``` | ||
|
||
### Templates | ||
Symfony allows to override Bundle templates by placing them in the `templates` directory. This is something we don't recommend! | ||
We, at CoreShop, sometimes change Templates, add templates, or rename them. Sometimes on accident, sometimes on purpose. | ||
|
||
To not run into any issues with CoreShop Demo Files to be loaded, we recommend to copy the Templates from the Demo Frontend. | ||
There is also a new command for that: | ||
|
||
```bash | ||
php bin/console coreshop:frontend:install | ||
``` | ||
|
||
This will copy all the templates from CoreShop into `coreshop` and it also replaces all the Bundle Prefixes. That way, you | ||
can be sure that you are using the correct templates. | ||
|
||
This command also creates a configuration to change the `TemplateConfigurator` to use your app template paths and not the | ||
FrontendBundle paths. | ||
|
||
## Headless | ||
If you are going headless, you can simply not enable the Frontend Bundle and do whatever need for your Headless API. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/CoreShop/Bundle/FrontendBundle/Command/InstallFrontendCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\FrontendBundle\Command; | ||
|
||
use CoreShop\Bundle\CoreBundle\Command\AbstractInstallCommand; | ||
use CoreShop\Bundle\CoreBundle\Installer\Checker\CommandDirectoryChecker; | ||
use CoreShop\Bundle\FrontendBundle\Installer\FrontendInstallerInterface; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
final class InstallFrontendCommand extends AbstractInstallCommand | ||
{ | ||
public function __construct( | ||
KernelInterface $kernel, | ||
CommandDirectoryChecker $directoryChecker, | ||
protected FrontendInstallerInterface $frontendInstaller, | ||
) { | ||
parent::__construct($kernel, $directoryChecker); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('coreshop:frontend:install') | ||
->setDescription('Install CoreShop Demo Frontend.') | ||
->setHelp( | ||
<<<EOT | ||
The <info>%command.name%</info> command install CoreShop Frontend Controllers/Templates/Configs. | ||
EOT | ||
) | ||
->addOption('templatePath', null, InputOption::VALUE_OPTIONAL, 'Path to the template directory', 'templates') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$coreBundle = $this->kernel->getBundle('CoreShopFrontendBundle'); | ||
$frontendBundlePath = $coreBundle->getPath(); | ||
|
||
$rootPath = $this->kernel->getProjectDir(); | ||
|
||
$templatePath = $rootPath . '/' . $input->getOption('templatePath'); | ||
|
||
$this->frontendInstaller->installFrontend( | ||
$frontendBundlePath, | ||
$rootPath, | ||
$templatePath | ||
); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/CoreShop/Bundle/FrontendBundle/Installer/FrontendInstaller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\FrontendBundle\Installer; | ||
|
||
class FrontendInstaller implements FrontendInstallerInterface | ||
{ | ||
public function __construct(private readonly \IteratorAggregate $installers) | ||
{ | ||
} | ||
|
||
public function installFrontend(string $frontendBundlePath, string $rootPath, string $templatePath): void | ||
{ | ||
foreach ($this->installers as $installer) { | ||
$installer->installFrontend($frontendBundlePath, $rootPath, $templatePath); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/CoreShop/Bundle/FrontendBundle/Installer/FrontendInstallerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\FrontendBundle\Installer; | ||
|
||
interface FrontendInstallerInterface | ||
{ | ||
public function installFrontend(string $frontendBundlePath, string $rootPath, string $templatePath): void; | ||
} |
52 changes: 52 additions & 0 deletions
52
src/CoreShop/Bundle/FrontendBundle/Installer/TemplateConfiguratorInstaller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\FrontendBundle\Installer; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class TemplateConfiguratorInstaller implements FrontendInstallerInterface | ||
{ | ||
public function installFrontend(string $frontendBundlePath, string $rootPath, string $templatePath): void | ||
{ | ||
$configFile = $rootPath . '/config/packages/coreshop_frontend.yaml'; | ||
|
||
$configContent = <<<CONFIG | ||
core_shop_frontend: | ||
view_prefix: 'coreshop' | ||
CONFIG; | ||
$fs = new Filesystem(); | ||
if (!file_exists($configFile)) { | ||
$fs->dumpFile($configFile, $configContent); | ||
return; | ||
} | ||
|
||
$configContent = file_get_contents($configFile); | ||
|
||
$content = Yaml::parse($configContent); | ||
|
||
if (!isset($content['core_shop_frontend']['view_prefix'])) { | ||
$content['core_shop_frontend']['view_prefix'] = 'coreshop'; | ||
} | ||
|
||
$configContent = Yaml::dump($content, 4, 2); | ||
$fs->dumpFile($configFile, $configContent); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/CoreShop/Bundle/FrontendBundle/Installer/TemplatesInstaller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\FrontendBundle\Installer; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class TemplatesInstaller implements FrontendInstallerInterface | ||
{ | ||
public function installFrontend(string $frontendBundlePath, string $rootPath, string $templatePath): void | ||
{ | ||
$finder = new Finder(); | ||
$finder | ||
->in($frontendBundlePath . '/Resources/views') | ||
->name('*.twig'); | ||
|
||
$twigFiles = $finder->files(); | ||
|
||
$fs = new Filesystem(); | ||
|
||
if (!$fs->exists($templatePath . '/coreshop')) { | ||
$fs->mkdir($templatePath . '/coreshop'); | ||
} | ||
|
||
foreach ($twigFiles as $twigFile) { | ||
$newFileName = $templatePath. '/coreshop/' . $twigFile->getRelativePathname(); | ||
|
||
if ($fs->exists($newFileName)) { | ||
continue; | ||
} | ||
|
||
$twigContent = file_get_contents($twigFile->getRealPath()); | ||
$twigContent = str_replace( | ||
array('@CoreShopFrontend/', 'bundles/coreshopfrontend/'), | ||
array('', 'coreshop/'), | ||
$twigContent | ||
); | ||
|
||
$fs->dumpFile($newFileName, $twigContent); | ||
} | ||
} | ||
} |
12 changes: 10 additions & 2 deletions
12
src/CoreShop/Bundle/FrontendBundle/Resources/config/services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/CoreShop/Bundle/FrontendBundle/Resources/config/services/frontend-installer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
services: | ||
|
||
CoreShop\Bundle\FrontendBundle\Installer\TemplatesInstaller: | ||
tags: | ||
- { name: coreshop.frontend.installer } | ||
|
||
CoreShop\Bundle\FrontendBundle\Installer\TemplateConfiguratorInstaller: | ||
tags: | ||
- { name: coreshop.frontend.installer } | ||
|
||
CoreShop\Bundle\FrontendBundle\Installer\FrontendInstallerInterface: '@CoreShop\Bundle\FrontendBundle\Installer\FrontendInstaller' | ||
CoreShop\Bundle\FrontendBundle\Installer\FrontendInstaller: | ||
arguments: | ||
- !tagged_iterator coreshop.frontend.installer |
Oops, something went wrong.