Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty and Factory Singleton (include code)

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
aragonesse
Smarty n00b


Joined: 11 Jul 2012
Posts: 1

PostPosted: Wed Jul 11, 2012 9:39 pm    Post subject: Smarty and Factory Singleton (include code) Reply with quote

Hi, im developer from Italy and sorry for my bad english.

This is my solution for unique instance of Smarty (Singleton)

Code:

<?php

/**
 *
 *            Package:            Smarty Singleton
 *            Author:              Adriano Giovannini
 *            Mail:                  klaimath@libero.it
 *            License:            GPL Version 3
 *
 */
require_once 'Smarty.class.php';

final class FactorySingletonSmarty extends Smarty
{

    /**
     * Private attribute for Smarty instance
     *
     * @return bool
     *
     */
    static private $_smartyInstance;

    /**
     *
     * Constructor
     *
     */
    public function __construct()
    {
   
        // Ridefined original constructor
        parent::__construct();

            // Set all pats
            $this->setTemplateDir('yor_dir_templates');
            $this->setCompileDir('yor_dir_templates_c');
            $this->setConfigDir('yor_dir_configs');
            $this->setCacheDir('yor_dir_cache');

}

    /**
     *
     * The Singleton instance
     *
     * @return $_SmartyInstance
     *
     */
    static public function getInstance()
    {

        /**
         *
         *   If instance not exits
         *
         */
        if (!self::$_smartyInstance)
        {

            try
            {

                /**
                 *
                 *   Create a new instances
                 *
                 */
                self::$_smartyInstance =  new FactorySingletonSmarty();

            }

                /**
                 *
                 * Excpetion
                 *
                 */
                catch(Excpetion $e)
            {

                    /**
                     *
                     * My definied method
                     *
                     */
                    self::printException($e);

            }

        }
             return self::$_smartyInstance;

    }

    /**
     *
     *        Error report
     *
     */
    static private function printException($e)
    {
   
        echo '<html><head><title>Error detailed info</title></head><body>';

            $trace = '<table border="0"><center>';
                foreach ($e->getTrace() as $file => $b) {
                    foreach ($b as $args => $d) {
                        if ($args == 'args') {
                            foreach ($d as $position => $details) {
                                $trace .= '<tr><td><b>' . strval($file) . '# </b></td><td align="right"><u>args: </u></td> <td><u>'
                              . $position . '</u> : </td><td><i>' . $details . '</i></td></tr>';
                            }       
                        } else {
                            $trace .= '<tr><td><b>' . strval($file) . '# </b></td><td align="right"><u>' . $args
                                   . '</u>:</td><td></td><td><i>' . $d . '</i></td>';
                        }
                    }
                }
                            $trace .= '</center></table>';
                            echo '<br /><br /><center><font face="Verdana"><fieldset style="width: 66%; border: 1px solid black;><legend background="black"><b>[</b>PHP PDO Error '
                                . strval($e->getCode()) . ' <b>]</b></legend><table border="0"><tr><td align="left"><b><u> Message: </u></b></td><td><i> ' . $e->getMessage()
                                . ' </i></td></tr><br /><tr><td align="left"><b><u>Code: </u></b></td><td><i> ' . strval($e->getCode())
                                . ' </i></td></tr><br /><tr><td align="left"><b><u>File: </u></b></td><td><i> ' . $e->getFile()
                                . ' </i></td></tr><br /><tr><td align="left"><b><u>Line: </u></b></td><td><i> ' . strval($e->getLine())
                                . ' </i></td></tr><br /><tr><td align="left"><b><u>Trace: <br /></u></b></td><td><br /><br />'
                                . $trace . '<br /></td></tr></table></fieldset></center></font>';
                            echo '</body></html>';
                            die();
       
    }

    /**
     *
     * Singleton debug.
     *
     */
    static public function debug()
    {
 
        $objSmarty = self::getInstance();
        $objSmartyTest = self::getInstance();
            if ($objSmarty === $objSmartyTest)
            {
           
                echo "The instaces are equals. Singleton work fine.";
       
            } else {
           
                echo "The instances not equals. Singleton not worksi";
       
            }
   
    }   

}
               
?>


configure.php
Code:

<?php

// Namespace
namespace FactorySmarty
{

   // Smarty debug
   const SMARTYDEBUG = false;

   // Singleton debug
   const SMARTYSINGLETON = true;

}
?>


How to use

index.php
Code:

<?php

require_once 'configure.php';
require_once 'Factory.class.php';

/**
 *
 *       Verify if SIngleton work
 *
 */
if (FactorySmarty\SMARTYSINGLETON == true) { FactorySingletonSmarty::debug(); }

try
{
   
   /**
    *
     *        Create a unique instance of smarty
     *
     */
    $objSmarty = FactorySingletonSmarty::getInstance();

    $objSmarty->assign('name', 'MyName');

    /**
     *
     * If smarty debug is active
     *
     */ 
    if (FactorySmarty\SMARTYDEBUG == true) { $objSmarty->debugging = true; }

    /**
     *
     * Rendering
     *
     */
    $objSmarty->display('index.tpl');

}

    catch (Excpetion $e)
   
{
   
    echo "An error occurred " . $e->getMessage() . "<br />";
   
}

?>


index.tpl
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>

<body>
{* Smarty *}
Hi {$name} welcome. This is a Smarty Template Engine
</body>
</html>


Is all.. any critique is appreciated
Adriano
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP