Skip to content

Commit

Permalink
under construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Sep 19, 2016
1 parent 7731a12 commit cb54e8a
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Cache/Storage.php
Expand Up @@ -17,7 +17,7 @@
*/
namespace Ytake\LaravelSmarty\Cache;

use Smarty;
use Ytake\LaravelSmarty\Smarty;
use Illuminate\Contracts\Config\Repository as ConfigContract;

/**
Expand Down
39 changes: 39 additions & 0 deletions src/Compilers/Compilable.php
@@ -0,0 +1,39 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2014-2016 Yuuki Takezawa
*
*/
namespace Ytake\LaravelSmarty\Compilers;

use Illuminate\Contracts\View\Factory;

/**
* Class Compilable
*
* @author yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
* @license http://opensource.org/licenses/MIT MIT
*/
interface Compilable
{
/**
* @param Factory $factory
*/
public function setViewFactory(Factory $factory);

/**
* @param array $data
*/
public function setLaravelViewData(array $data);
}
97 changes: 97 additions & 0 deletions src/Compilers/CompileInclude.php
@@ -0,0 +1,97 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2014-2016 Yuuki Takezawa
*
*/
namespace Ytake\LaravelSmarty\Compilers;

use Illuminate\View\View;
use Illuminate\Contracts\View\Factory;

/**
* Class CompileInclude
*
* @author yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
* @license http://opensource.org/licenses/MIT MIT
*/
class CompileInclude extends \Smarty_Internal_Compile_Include implements Compilable
{
/** @var Factory|\Ytake\LaravelSmarty\SmartyFactory */
protected $viewFactory;

/** @var array */
protected $laravelViewData;

/**
* {@inheritdoc}
*/
public function compile($args, \Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter)
{
foreach ($args as $arg) {
if (isset($arg['file'])) {
$viewName = $this->normalizeTemplateName($arg['file']);
$this->dispatch($viewName);
}
}

return parent::compile($args, $compiler, $parameter);
}

/**
* @param Factory $factory
*/
public function setViewFactory(Factory $factory)
{
$this->viewFactory = $factory;
}

/**
* @param array $data
*/
public function setLaravelViewData(array $data)
{
$this->laravelViewData = $data;
}

/**
* @param string $name
*
* @return string
*/
protected function normalizeTemplateName($name)
{
$fileInfo = new \SplFileInfo($name);
$path = ($fileInfo->getPath() === '') ? null : $fileInfo->getPath() . '/';
$viewPathInfo = $path . $fileInfo->getBasename('.' . $fileInfo->getExtension());

return trim(str_replace('/', '.', $viewPathInfo), '"');
}

/**
* @param $name
*/
protected function dispatch($name)
{
$view = new View(
$this->viewFactory,
$this->viewFactory->getEngineResolver()->resolve('smarty'),
$name,
null,
[]
);
$this->viewFactory->callCreator($view);
$this->viewFactory->callComposer($view);
}
}
84 changes: 84 additions & 0 deletions src/Compilers/TemplateCompiler.php
@@ -0,0 +1,84 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2014-2016 Yuuki Takezawa
*
*/
namespace Ytake\LaravelSmarty\Compilers;

/**
* Class TemplateCompiler
*
* @author yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
* @license http://opensource.org/licenses/MIT MIT
*/
class TemplateCompiler extends \Smarty_Internal_SmartyTemplateCompiler
{
/**
* {@inheritdoc}
*/
public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
{
$this->smarty->tpl_vars['userCount'] = new \Smarty_Variable(9999);
if (!isset(self::$_tag_objects[$tag])) {
// lazy load internal compiler plugin
$_tag = explode('_', $tag);
$_tag = array_map('ucfirst', $_tag);
$class_name = $this->detectSmartyInternalCompiler('Smarty_Internal_Compile_' . implode('_', $_tag));
if (class_exists($class_name) &&
(!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))
) {
self::$_tag_objects[$tag] = $this->appendViewFactoryInstance(new $class_name);;
} else {
self::$_tag_objects[$tag] = false;

return false;
}
}

// compile this tag
return self::$_tag_objects[$tag] === false ? false :
self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
}

/**
* @param string $className
*
* @return string
*/
protected function detectSmartyInternalCompiler($className)
{
if ($className === \Smarty_Internal_Compile_Include::class) {
$className = CompileInclude::class;
}

return $className;
}

/**
* @param \Smarty_Internal_CompileBase|Compilable $instance
*
* @return \Smarty_Internal_CompileBase|Compilable
*/
protected function appendViewFactoryInstance($instance)
{
if ($instance instanceof Compilable) {
if (!is_null($this->smarty->getViewFactory())) {
$instance->setViewFactory($this->smarty->getViewFactory());
}
}

return $instance;
}
}
2 changes: 1 addition & 1 deletion src/Console/CacheClearCommand.php
Expand Up @@ -17,7 +17,7 @@
*/
namespace Ytake\LaravelSmarty\Console;

use Smarty;
use Ytake\LaravelSmarty\Smarty;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

Expand Down
2 changes: 1 addition & 1 deletion src/Console/OptimizeCommand.php
Expand Up @@ -17,7 +17,7 @@
*/
namespace Ytake\LaravelSmarty\Console;

use Smarty;
use Ytake\LaravelSmarty\Smarty;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Contracts\Config\Repository as ConfigContract;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/PackageInfoCommand.php
Expand Up @@ -17,7 +17,7 @@
*/
namespace Ytake\LaravelSmarty\Console;

use Smarty;
use Ytake\LaravelSmarty\Smarty;
use Illuminate\Console\Command;
use Ytake\LaravelSmarty\SmartyFactory;

Expand Down
4 changes: 3 additions & 1 deletion src/Engines/SmartyEngine.php
Expand Up @@ -17,8 +17,9 @@
*/
namespace Ytake\LaravelSmarty\Engines;

use Smarty;
use Illuminate\Events\Dispatcher;
use Throwable;
use Ytake\LaravelSmarty\Smarty;
use Illuminate\View\Engines\EngineInterface;
use Symfony\Component\Debug\Exception\FatalThrowableError;

Expand Down Expand Up @@ -83,6 +84,7 @@ protected function evaluatePath($path, array $data = [])

/**
* @codeCoverageIgnore
*
* @param \Exception $e
*
* @throws \Exception
Expand Down
64 changes: 64 additions & 0 deletions src/Engines/SmartyTemplate.php
@@ -0,0 +1,64 @@
<?php

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2014-2016 Yuuki Takezawa
*
*/
namespace Ytake\LaravelSmarty\Engines;

use Smarty;
use Smarty_Internal_Data;
use Smarty_Template_Source;
use Ytake\LaravelSmarty\Compilers\TemplateCompiler;

/**
* Class SmartyTemplate
*
* @author yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
* @license http://opensource.org/licenses/MIT MIT
*/
class SmartyTemplate extends \Smarty_Internal_Template
{
/**
* {@inheritdoc}
*/
public function __construct(
$template_resource,
Smarty $smarty,
Smarty_Internal_Data $_parent = null,
$_cache_id = null,
$_compile_id = null,
$_caching = null,
$_cache_lifetime = null
) {
$this->smarty = $smarty;
$this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
$this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
$this->caching = $_caching === null ? $this->smarty->caching : $_caching;
if ($this->caching === true) {
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
}
$this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
$this->parent = $_parent;
// Template resource
$this->template_resource = $template_resource;
$this->source = Smarty_Template_Source::load($this);
$this->source->compiler_class = TemplateCompiler::class;

Smarty_Internal_Data::__construct();
if ($smarty->security_policy && method_exists($smarty->security_policy, 'registerCallBacks')) {
$smarty->security_policy->registerCallBacks($this);
}
}
}

0 comments on commit cb54e8a

Please sign in to comment.