| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
5 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
6 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
7 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
8 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
9 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
10 |
* THE SOFTWARE.
|
|
|
11 |
*
|
|
|
12 |
* This software consists of voluntary contributions made by many individuals
|
|
|
13 |
* and is licensed under the MIT license.
|
|
|
14 |
*
|
|
|
15 |
* Copyright (c) 2014-2022 Yuuki Takezawa
|
|
|
16 |
*
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
declare(strict_types=1);
|
|
|
20 |
|
|
|
21 |
namespace Ytake\LaravelSmarty;
|
|
|
22 |
|
|
|
23 |
use Illuminate\Contracts\Container\BindingResolutionException;
|
|
|
24 |
use Illuminate\Contracts\Foundation\Application;
|
|
|
25 |
use Illuminate\Support\ServiceProvider;
|
|
|
26 |
use SmartyException;
|
|
|
27 |
|
|
|
28 |
use const DIRECTORY_SEPARATOR;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Class LaravelSmartyServiceProvider
|
|
|
32 |
*
|
|
|
33 |
* @author yuuki.takezawa<yuuki.takezawa@comnect.jp.net>
|
|
|
34 |
* @license http://opensource.org/licenses/MIT MIT
|
|
|
35 |
*/
|
|
|
36 |
class SmartyServiceProvider extends ServiceProvider
|
|
|
37 |
{
|
|
|
38 |
/**
|
|
|
39 |
* boot
|
|
|
40 |
* @throws BindingResolutionException
|
|
|
41 |
*/
|
|
|
42 |
public function boot(): void
|
|
|
43 |
{
|
|
|
44 |
// add Smarty Extension
|
|
|
45 |
$extension = $this->app['config']->get('ytake-laravel-smarty.extension', 'tpl');
|
|
|
46 |
$this->app['view']->addExtension($extension, 'smarty', function () {
|
|
|
47 |
// @codeCoverageIgnoreStart
|
|
|
48 |
$smarty = $this->app->make('smarty.view');
|
|
|
49 |
|
|
|
50 |
return new Engines\SmartyEngine($smarty->getSmarty());
|
|
|
51 |
// @codeCoverageIgnoreEnd
|
|
|
52 |
});
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* {@inheritdoc}
|
|
|
57 |
*/
|
|
|
58 |
public function register(): void
|
|
|
59 |
{
|
|
|
60 |
$configPath = __DIR__ . '/config/ytake-laravel-smarty.php';
|
|
|
61 |
$this->mergeConfigFrom($configPath, 'ytake-laravel-smarty');
|
|
|
62 |
$this->publishes([
|
|
|
63 |
$configPath => $this->resolveConfigurePath()
|
|
|
64 |
. DIRECTORY_SEPARATOR
|
|
|
65 |
. 'ytake-laravel-smarty.php',
|
|
|
66 |
]);
|
|
|
67 |
$this->app->singleton('smarty.view', fn ($app) => $this->createSmarty($app));
|
|
|
68 |
$this->app->alias('smarty.view', SmartyFactory::class);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @return string
|
|
|
73 |
*/
|
|
|
74 |
protected function resolveConfigurePath(): string
|
|
|
75 |
{
|
|
|
76 |
return (isset($this->app['path.config']))
|
|
|
77 |
? (string)$this->app['path.config'] : $this->app->basePath() . DIRECTORY_SEPARATOR . 'config';
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* @param Application $app
|
|
|
82 |
* @return SmartyFactory
|
|
|
83 |
* @throws SmartyException
|
|
|
84 |
*/
|
|
|
85 |
public function createSmarty(Application $app): SmartyFactory
|
|
|
86 |
{
|
|
|
87 |
$smartyTemplate = new Smarty();
|
|
|
88 |
$factory = new SmartyFactory(
|
|
|
89 |
$app['view.engine.resolver'],
|
|
|
90 |
$app['view.finder'],
|
|
|
91 |
$app['events'],
|
|
|
92 |
$smartyTemplate,
|
|
|
93 |
$this->app['config']
|
|
|
94 |
);
|
|
|
95 |
// Pass the container to the factory so it can be used to resolve view composers.
|
|
|
96 |
$factory->setContainer($app);
|
|
|
97 |
$factory->share('app', $app);
|
|
|
98 |
// resolve cache storage
|
|
|
99 |
$factory->resolveSmartyCache();
|
|
|
100 |
// smarty configure(use ytake-laravel-smarty.php)
|
|
|
101 |
$factory->setSmartyConfigure();
|
|
|
102 |
$smartyTemplate->setViewFactory($factory);
|
|
|
103 |
return $factory;
|
|
|
104 |
}
|
|
|
105 |
}
|