<repo_name>FuturaSoft/pabana<file_sep>/src/Mvc/Controller.php
<?php
/**
 * Pabana : PHP Framework (https://pabana.futurasoft.fr)
 * Copyright (c) FuturaSoft (https://futurasoft.fr)
 *
 * Licensed under BSD-3-Clause License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) FuturaSoft (https://futurasoft.fr)
 * @link          https://pabana.futurasoft.fr Pabana Project
 * @since         1.0
 * @license       https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause License
 */
namespace Pabana\Mvc;

use Pabana\Core\Configuration;
use Pabana\Html\Html;
use Pabana\Mvc\Layout;
use Pabana\Mvc\Model;
use Pabana\Mvc\View;
use Pabana\Network\Http\Request;
use Pabana\Type\StringType;

/**
 * Controller class
 *
 * Launch controller and call Layout and View if defined
 */
class Controller
{
    /**
     * @var     Redirection to $html var
     * @since   1.0
     * @deprecated deprecated since version 1.1
     */
    public $Html;

    /**
     * @var     Redirection to $layout var
     * @since   1.0
     * @deprecated deprecated since version 1.1
     */
    public $Layout;

    /**
     * @var     Redirection to $model var
     * @since   1.0
     * @deprecated deprecated since version 1.1
     */
    public $Model;

    /**
     * @var     Redirection to $request var
     * @since   1.0
     * @deprecated deprecated since version 1.1
     */
    public $Request;

    /**
     * @var     Redirection to $view var
     * @since   1.0
     * @deprecated deprecated since version 1.1
     */
    public $View;

    /**
     * @var     \Pabana\Html\Html Object Html.
     * @since   1.1
     */
    public $html;

    /**
     * @var     \Pabana\Mvc\Layout Object Layout.
     * @since   1.1
     */
    public $layout;

    /**
     * @var     \Pabana\Mvc\Model Object Model.
     * @since   1.1
     */
    public $model;

    /**
     * @var     \Pabana\Network\Http\Request Object Request.
     * @since   1.1
     */
    public $request;

    /**
     * @var     \Pabana\Mvc\View Object View.
     * @since   1.1
     */
    public $view;

    /**
     * @var     string Controller name.
     * @since   1.1
     */
    private $controller;

    /**
     * Constructor
     *
     * Initialize Controller helper object (Html, Layout, Model, Request, View)
     *
     * @since   1.1
     * @return  void
     */
    final public function __construct()
    {
        $controllerNamespace = get_class($this);
        $controllerString = new StringType($controllerNamespace);
        // Get controller by current class name
        $this->controller = $controllerString->classBasename();
        $this->html = new Html();
        $this->model = new Model();
        $this->request = new Request();
        // To maintain compatibility with version 1.0
        $this->Html = $this->html;
        $this->Model = $this->model;
        $this->Request = $this->request;
    }

    /**
     * Create the View and the Layout
     * Call initialize method if exist
     * Launch controller
     * Get Layout and View render if enable
     *
     * @since   1.1
     * @param   string $action Name of Action
     * @return  string bodyContent of Controller, Layout and View
     */
    final public function render($action)
    {
        // Initialize view object if auto render is enable
        if (Configuration::read('mvc.view.auto_render') === true) {
            $this->setView($this->controller, $action);
        }
        // Initialize layout object if auto render is enable
        if (Configuration::read('mvc.layout.auto_render') === true) {
            // Get Layout default name from Configuration
            $layoutName = Configuration::read('mvc.layout.default');
            $this->setLayout($layoutName);
        }
        // Call initialize method in Controller if exists
        if (method_exists($this, 'initialize')) {
            $this->initialize();
        }
        // Launch action of controller
        ob_start();
        $actionResult = $this->$action();
        echo PHP_EOL;
        $bodyContent = ob_get_clean();
        // If Controller's Action return false, disable Layout and View
        if ($actionResult !== false) {
            if (isset($this->layout) && $this->layout->getAutoRender()) {
                // Get Layout and View if auto render of View enable
                $bodyContent .= $this->layout->render();
            } elseif (isset($this->view) && $this->view->getAutoRender()) {
                // Get View only
                $bodyContent .= $this->view->render();
            }
        }
        return $bodyContent;
    }

    /**
     * Call Layout object and store it in $this->layout variable
     *
     * @since   1.1
     * @param   string $layoutName Name of Layout
     * @return  void
     */
    final public function setLayout($layoutName)
    {
        $layoutNamespace = Configuration::read('mvc.layout.namespace');
        $layoutNamespace = $layoutNamespace . '\\' . $layoutName;
        $this->layout = new $layoutNamespace($this->view);
        // To maintain compatibility with version 1.0
        $this->Layout = $this->layout;
    }

    /**
     * Call Layout object and store it in $this->view variable
     * If Layout is already defined, change view of $this->layout
     *
     * @since   1.1
     * @param   string $action Target action name
     * @return  void
     */
    final public function setView($controller, $action)
    {
        $this->view = new View($controller, $action);
        // To maintain compatibility with version 1.0
        $this->View = $this->view;
        // If Layout is enable, send new view to it
        if (isset($this->layout)) {
            $this->layout->setView($this->view);
            // To maintain compatibility with version 1.0
            $this->Layout = $this->layout;
        }
    }
}
<file_sep>/src/Database/Connection.php
<?php
/**
 * Pabana : PHP Framework (https://pabana.futurasoft.fr)
 * Copyright (c) FuturaSoft (https://futurasoft.fr)
 *
 * Licensed under BSD-3-Clause License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) FuturaSoft (https://futurasoft.fr)
 * @link          https://pabana.futurasoft.fr Pabana Project
 * @since         1.0
 * @license       https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause License
 */
namespace Pabana\Database;

use Pabana\Database\Statement;

/**
 * Connection class
 *
 * Defined a connection to a database from a datasource
 */
class Connection
{
    /**
     * @var     Redirection to $datasource var
     * @since   1.1
     * @deprecated deprecated since version 1.1
     */
    public $Datasource;

    /**
     * @var     \Pabana\Database\Datasource Object Datasource.
     * @since   1.1
     */
    public $datasource;

    /**
     * @var     \PDO Object PDO created when connection is effectued
     * @since   1.0
     */
    private $pdo;

    /**
     * @var     string Connection name (by default same as Datasource)
     * @since   1.0
     */
    private $name;

    /**
     * Constructor
     *
     * Store Datasource object, define connection name and do connection
     *
     * @since   1.0
     * @param   \Pabana\Database\Datasource $datasource Object defined a datasource and its parameters.
     * @param   string $name Connection name.
     * @param   bool $autoConnect If defined connection will do.
     */
    public function __construct($datasource, $name = '', $autoConnect = true)
    {
        $this->datasource = $datasource;
        // To maintain compatibility with version 1.0
        $this->Datasource = $this->datasource;
        if (empty($name) === true) {
            $name = $this->datasource->getName();
        }
        $this->setName($name);
        if ($autoConnect === true) {
            $this->connect();
        }
    }

    /**
     * Do a connection
     *
     * Do a connection to a database from datasource parameter (DSN) via PDO object
     *
     * @since   1.0
     * @return  bool True if success or false.
     */
    public function connect()
    {
        try {
            $this->pdo = new \PDO(
                $this->datasource->getDsn(),
                $this->datasource->getUser(),
                $this->datasource->getPassword(),
                $this->datasource->getOption()
            );
            return true;
        } catch (PDOException $e) {
            throw new \Exception($e->getMessage());
            return false;
        }
    }

    /**
     * Initiates a transaction
     *
     * @since   1.1
     * @return  bool True if success or false.
     */
    public function beginTransaction()
    {
        // Check if connection is open
        if ($this->isConnected()) {
            $this->pdo->beginTransaction();
            return true;
        } else {
            return false;
        }
    }

    /**
     * Commits a transaction
     *
     * @since   1.1
     * @return  bool True if success or false.
     */
    public function commit()
    {
        // Check if connection is open
        if ($this->isConnected()) {
            $this->pdo->commit();
            return true;
        } else {
            return false;
        }
    }

    /**
     * Close a connection
     *
     * Close a connection to a database and destroy PDO object
     *
     * @since   1.0
     * @return  bool True if success or false.
     */
    public function disconnect()
    {
        // Check if connection is open
        if ($this->isConnected()) {
            // Destroy PDO object
            $this->pdo = null;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Execute an SQL statement
     *
     * Execute an SQL statement and return the number of affected rows
     *
     * @since   1.0
     * @param   string $query SQL Statement.
     * @return  bool|integer Return an integer with number of affected rows or return false if error.
     */
    public function exec($query)
    {
        if ($this->isConnected()) {
            try {
                return $this->pdo->exec($query);
            } catch (PDOException $e) {
                throw new \Exception($e->getMessage());
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Get PDO Object
     *
     * Return current PDO object use by this connection
     *
     * @since   1.0
     * @return  bool|\PDO Return current PDO object use by this connection or return false if error.
     */
    public function getPdoObject()
    {
        if ($this->isConnected()) {
            return $this->pdo;
        } else {
            return false;
        }
    }

    /**
     * Get Connection name
     *
     * Return current connection name
     *
     * @since   1.0
     * @return  string Return current connection name.
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Check if connection is establised
     *
     * @since   1.0
     * @return  bool Return true if connection is establised or return false.
     */
    public function isConnected()
    {
        return isset($this->pdo);
    }

    /**
     * Check if actual request is transaction
     *
     * @since   1.1
     * @return  bool Return true if actual request is a transaction
     */
    public function isTransaction()
    {
        if ($this->isConnected()) {
            return $this->pdo->inTransaction();
        } else {
            return false;
        }
    }

    /**
     * Get last insert ID
     *
     * Returns the ID of the last inserted row or sequence value
     *
     * @since   1.0
     * @return  bool|integer Returns the ID of the last inserted row or sequence value
     */
    public function lastInsertId()
    {
        if ($this->isConnected()) {
            try {
                return $this->pdo->lastInsertId();
            } catch (PDOException $e) {
                throw new \Exception($e->getMessage());
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Executes an SQL statement and return result
     *
     * Executes an SQL statement, returning a result set as a \Pabana\Database\Statement object
     *
     * @since   1.0
     * @param   string $query SQL Statement.
     * @return  bool|\Pabana\Database\Statement Returns Statement object or false if error
     */
    public function query($query)
    {
        if ($this->isConnected()) {
            try {
                $statement = $this->pdo->query($query);
                return new Statement($statement);
            } catch (PDOException $e) {
                throw new \Exception($e->getMessage());
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Rolls back a transaction
     *
     * @since   1.1
     * @return  bool True if success or false.
     */
    public function rollBack()
    {
        // Check if connection is open
        if ($this->isConnected()) {
            $this->pdo->rollBack();
            return true;
        } else {
            return false;
        }
    }

    /**
     * Set Connection name
     *
     * @since   1.0
     * @param   string $name Connection name.
     * @return  void
     */
    public function setName($name)
    {
        $this->name = $name;
    }
}
<file_sep>/src/Mvc/Model.php
<?php
/**
 * Pabana : PHP Framework (https://pabana.futurasoft.fr)
 * Copyright (c) FuturaSoft (https://futurasoft.fr)
 *
 * Licensed under BSD-3-Clause License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) FuturaSoft (https://futurasoft.fr)
 * @link          https://pabana.futurasoft.fr Pabana Project
 * @since         1.0
 * @license       https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause License
 */
namespace Pabana\Mvc;

use Pabana\Core\Configuration;
use Pabana\Database\ConnectionCollection;

/**
 * Model class
 *
 * Manage Model
 */
class Model
{
    /**
     * @var     Redirection to $connection var.
     * @since   1.0
     * @deprecated deprecated since version 1.1
     */
    public $Connection;

    /**
     * @var     \Pabana\Database\Connection Object Connection (default connection).
     * @since   1.1
     */
    public $connection;

    /**
     * Initialize model
     *
     * If default Connection is defined, call it in $Connection var.
     *
     * @since   1.0
     * @return  void
     */
    public function __construct()
    {
        if (ConnectionCollection::existsDefault() === true) {
            $this->connection = ConnectionCollection::getDefault();
            // To maintain compatibility with version 1.0
            $this->Connection = $this->connection;
        }
    }

    /**
     * Call a model class
     *
     * @since   1.0
     * @param   string $modelName Model class name
     * @return  object|bool Return model defined in $modelName or false if error
     */
    public function get($modelName)
    {
        $modelNamespace = Configuration::read('mvc.model.namespace');
        $modelNamespace = $modelNamespace . '\\' . ucFirst($modelName);
        if (!class_exists($modelNamespace)) {
            trigger_error('Model "' . $modelNamespace . '" doesn\'t exist.', E_USER_ERROR);
            return false;
        }
        return new $modelNamespace();
    }
}
