Witam, wczoraj starałem się stworzyć swój pierwszy framework w PHP, lecz napotkałem kilka problemów. Z racji, że nie jestem profesjonalistą proszę o wyrozumiałość. Od razu chcę zaznaczyć, że jeśli ktoś chce napisać posta w stylu: "wyjdź z tym i idź się ucz symfony/cakephp" to proszę, aby się nie fatygował bo po prostu to zignoruje. Chce budować aplikacje na własnym silniku, który bardzo dobrze znam i mogę go dowolne rozbudować itp. Poza tym mam też inne powody, których nie będę tutaj opisywał. Chce po prostu używać swojego i tyle.
Zrobiłem go na takiej zasadzie:
struktura folderów i plików:
.htaccess
index.php
[folder]conf
Dispatcher.php
_init.php
Model.php
Database.php
Autoloader.php
[folder]lib:
debugClass.php
[folder]controllers:
[folder]index:
Controller.php
[folder]cache
Plik .htaccess - zadanie kierowania wszelkiego ruchu do index.php:
RewriteEngine On
# Przekierowanie całego ruchu do index.php
RewriteRule (.*) index.php [L]
Plik index.php - załączenie frameworka z innego katalogu:
<?php
// Uruchamienie aplikacji
require_once('system/_init.php');
// Wyłączanie parsera
system/_init.php - główny plik frameworku:
<?php
// Definicje
// Autoloader
require(INC_PATH.'system/Autoloader.php');
// Debug
$debug = new Debug;
// Załaczanie niezbędnych plików
$debug->drequire(INC_PATH.'system/Dispatcher.php');
$debug->drequire(INC_PATH.'system/Model.php');
$debug->drequire(INC_PATH.'system/Database.php');
// Uruchamianie
$dispatcher = new Dispatcher;
$dispatcher->_init();
system/Dispatcher.php - przetwarza ścieżkę wpisaną przez usera:
<?php
class Dispatcher
{
private $_strUrlParameters;
private $_arrUrlParameters;
private $_index = FALSE;
public function __construct()
{
if (isset($_SERVER['PATH_INFO'])) {
$strUrlParameters = $_SERVER['PATH_INFO'];
$this->_strUrlParameters
= substr($strUrlParameters, 1
); }
else
{
$strUrlParameters = $_SERVER['REQUEST_URI'];
$this->_strUrlParameters
= substr($strUrlParameters, 1
); }
}
public function _init()
{
$this->_parsePath();
$strControllerName = $this->_getControllerName();
$strControllerFile = INC_PATH.'controllers/'.$this->_getControllerName().'/Controller.php';
{
require_once($strControllerFile);
$strControllerClasName = $this->_getControllerName().'Controller';
$objController = new $strControllerClasName($this->_getParametersForController());
$NumOfTrans = count($this->transfer); $objController->_init();
}
elseif ($this->index == FALSE)
{
require_once(INC_PATH.'controllers/index/Controller.php');
$objController = new indexController($this->_getParametersForController());
$objController->_init();
}
}
private function _parsePath()
{
$arrParameters = explode('/', $this->_strUrlParameters
); if($arrParameters[count($arrParameters) - 1] == '') $this->_arrUrlParameters = $arrParameters;
}
private function _getControllerName()
{
$strControllerName = $this->_arrUrlParameters[0];
if(strlen($strControllerName)) return $strControllerName;
elseif ($this->index == FALSE)
{
$this->index = TRUE;
require_once(INC_PATH.'controllers/index/Controller.php');
$objController = new indexController($this->_getParametersForController());
$objController->_init();
}
}
private function _getParametersForController()
{
$intNumOfPathParameters = count($this->_arrUrlParameters
); $arrPrametersForController = array(); for($i = 1; $i < $intNumOfPathParameters; $i++)
$arrPrametersForController[] = $this->_arrUrlParameters[$i];
return $arrPrametersForController;
}
}
system/Model.php - plik pusty, jeszcze nic tam nie wrzuciłem.
system/Database.php - sterownik bazy danych.
<?php
DEFINE('CACHE_DIR', 'cache/');
class Database {
public $server;
public $answer;
var $rows;
var $queries = 0;
var $cache_state = 0;
var $cache_file;
var $cache_buffer;
var $cache_ptr;
public function CONNECT ($HOST,$USER,$PASSWORD,$DATABASE) {
}
public function POLISH_CHARACTERS () {
}
public function SELECT ($query) {
return $this->answer = $array;
}
public function VAILD ($query) {
if ($qty >= 1) $return = TRUE;
else $return = FALSE;
return $this->answer = $return;
}
public function QUERY ($query) {
return $this->answer = $answer;
}
public function VAILD_QUERY ($query) {
else $return = FALSE;
return $this->answer = $return;
}
public function sql_cache($handle = 0) {
if(file_exists(CACHE_DIR
.'sql_cache-'.$handle.'.cache')) { $this->cache_state = 1;
$this->cache_ptr = 0;
}
else {
$this->cache_state = 2;
$this->cache_buffer = array(); $this->cache_file = CACHE_DIR.'sql_cache-'.$handle.'.cache';
}
}
else {
if($this->cache_state == 2) {
file_put_contents
($this->cache_file, serialize($this->cache_buffer)); }
$this->cache_state = 0;
}
}
function sql_cache_remove($handle) {
if(file_exists(CACHE_DIR
.'sql_cache-'.$handle.'.cache')) { unlink(CACHE_DIR
.'sql_cache-'.$handle.'.cache'); }
}
function cache_query($query) {
if($this->cache_state != 1) {
return 1;
}
}
function sql_fetch_array() {
if($this->cache_state == 1) {
if(!isset($this->cache_buffer[$this->cache_ptr])) { return 0;
}
$this->rows = $this->cache_buffer[$this->cache_ptr];
$this->cache_ptr++;
return 1;
}
else {
if($this->cache_state == 2) {
$this->cache_buffer[] = $this->rows;
}
return 1;
}
}
return 0;
}
function sql_fetch_row() {
if($this->cache_state == 1) {
if(!isset($this->cache_buffer[$this->cache_ptr])) { return 0;
}
$this->rows = $this->cache_buffer[$this->cache_ptr];
$this->cache_ptr++;
return 1;
}
else {
if($this->cache_state == 2) {
$this->cache_buffer[] = $this->rows;
}
return 1;
}
}
return 0;
}
}
?>
system/Autoloader.php - ładuje biblioteki z folderu:
<?php
function __autoload ($classname)
{
require_once(INC_PATH.'lib/'.$classname.'Class.php');
}
lib/debugClass.php - liczy jak długo parseruje się dana klasa:
<?php
function getMicrotime()
{
return ((float)$usec + (float)$sec);
}
class Debug
{
private $requireArray = array();
public function drequire($dir)
{
$this->requireArray[$dir] = getMicrotime();
require_once($dir);
$this->requireArray[$dir] = getMicrotime()-$this->requireArray[$dir];
}
public function RequireTime ()
{
return $this->requireArray;
}
}
contollers/index/Controller.php - plik kontrolera index:
<?php
class indexController
{
var $var;
function __CONSTRUCT ($var)
{
$this->var = $var[0];
}
public function _init()
{
if ($this->var == "test")
echo "<b>Wszystko działa poprawnie!</b>"; elseif ($this->var == "debug")
{
}
else
echo "<b>Hello world!</b>"; }
}
Prosiłbym o rady takie, które odpowiedzą mi na pewne pytania:
- Czy dobrze zbudowałem cały framework? Coś powinienem zmienić?
- Mam taki problem, że gdy chce np. wykonać akcje localhost/index/debug to dostaje błąd, że $debug nie jest obiektem. Wiem o tym, ale jak stworze go na nowo, to nie będzie miał już tej tablicy z danymi. Jak to rozmwiać?
- Inne uwagi według własnego uznania mile widziane.
Ten post edytował pajaa1981 16.01.2011, 17:35:14