login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Grote systemen > PHP Framework

PHP Framework

Auteur: Raze - 06 april 2010 - 22:27 - Gekeurd door: Joel - Hits: 4199 - Aantal punten: 0.50 (2 stemmen)




LEES DE UPDATES BIJ EDIT 2!!
Dit is een basis framework in php. Het is gebaseerd op de volgende tutorials: http://net.tuts...ork-part-1/. Het is wel géén kopie, er zijn andere dingen aan toegevoegd (de echte basis is wel hetzelfde).
De opbouw van de mappen:

MAP /framework/
- framework.class.php
- exceptions.class.php
MAP /framework/objects/
- database.class.php
- validator.class.php

Het is vooral een voorbeeld, hoewel je er ook wel al een (basis) website mee kan bouwen. Je kan natuurlijk ook extra dingen toevoegen. De klasse validator is een voorbeeld: het heeft momenteel maar één functie (om een e-mailadres te controleren).

Het is echt een basis script, niet gelijk bv. zend framework (ZF is dan ook door meerdere mensen gebouwd).

EDIT (2010-04-09):
- Ik heb nu ook een postgresql versie gemaakt van de database klasse. De rest blijft hetzelfde.
EDIT 2 ( 2010-05-02):
- Het script is volledig veranderd, en is nu niet meer gebaseerd op de tutorials in de link, maar ik heb het nu zelf proberen te verzinnen.
- Er zit alleen nog maar een postgresql-versie in van de database-klasse, moest je graag een mysql-versie hebben kan je me steeds PM'en dan help ik je wel verder.
- De validator-klasse is verwijderd. Dit was toch slechts een voorbeeld.
- De mapstructuur:
/framework/
- framework.class.php
/objects/
- database.class.php
/exceptions/
- framework.exc.php

Code:
Voorbeeld van gebruik:
  1. <?php
  2. try {
  3. $fw = framework::initialize();
  4.  
  5. $key = $fw->useClass('db')->initConnection( "HOSTNAME" , "PORT" , "DATABASE" , "USERNAME" , "PASSWORD" );
  6. $fw->useClass('db')->closeConnection( $key );
  7. }
  8. catch( fw_exception $e )
  9. {
  10. echo $e;
  11. };
  12. ?>


framework.class.php
  1. <?php
  2.  
  3. class framework
  4. {
  5. const FW_NAME = "Framework name";
  6.  
  7. const FW_VERSION = "1.0";
  8.  
  9. public $data = array();
  10.  
  11. public $objects = array();
  12.  
  13. private static $instance = null;
  14.  
  15. public function __construct()
  16. {
  17. require_once( "exceptions/framework.exc.php" );
  18.  
  19. /**
  20. * Register core classes
  21. */
  22. $this->registerClass( "db" , "database" );
  23. }
  24.  
  25. public function __clone()
  26. {
  27. throw new fw_exception( "Cloning is not allowed" );
  28. }
  29.  
  30. public static function initialize()
  31. {
  32. if( is_null( self::$instance ) )
  33. {
  34. self::$instance = new self;
  35. }
  36.  
  37. return self::$instance;
  38. }
  39.  
  40. public function registerClass( $key , $classname )
  41. {
  42. $file = "objects/".$classname.".class.php";
  43.  
  44. if( is_file( $file ) )
  45. {
  46. require_once( $file );
  47. $this->objects[$key] = new $classname;
  48. }
  49. else
  50. {
  51. throw new fw_exception( "Class not found" );
  52. }
  53. }
  54.  
  55. public function useClass( $key )
  56. {
  57. return $this->objects[$key];
  58. }
  59.  
  60. public function putData( $value )
  61. {
  62. $this->data[] = $value;
  63.  
  64. return count($this->data)-1;
  65. }
  66.  
  67. public function getData( $key )
  68. {
  69. if( is_numeric( $key ) )
  70. {
  71. return $this->data[$key];
  72. }
  73. else
  74. {
  75. throw new fw_exception( "Key value is not valid (not an integer)" );
  76. }
  77. }
  78.  
  79. public function getFWdata( $datatype )
  80. {
  81. switch( $datatype )
  82. {
  83. case "FW_NAME":
  84. return self::FW_NAME;
  85. break;
  86. case "FW_VERSION":
  87. return self::FW_VERSION;
  88. break;
  89. }
  90. }
  91. }
  92.  
  93. ?>


database.class.php
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * Class database
  6.  *
  7.  * public function initConnection( $hostname , $port , $database , $username , $password )
  8.  * public function setActiveConnection( $key )
  9.  * public function select( $items , $table , $condition = "" )
  10.  * public function escapeString( $input )
  11.  * public function numRows( $key )
  12.  * public function fetchArray( $key )
  13.  * public function insert( $table , $input )
  14.  * public function update( $table , $input , $condition )
  15.  * public function delete( $table , $condition )
  16.  * public function executeQuery( $querystring )
  17.  * public function affectedRows( $key )
  18.  * public function closeConnection( $key = NULL )
  19.  */
  20.  
  21. class database
  22. {
  23. public $connections = array();
  24.  
  25. public $activeConnection;
  26.  
  27. public $resultset = array();
  28.  
  29. /**
  30. *
  31. * @access public
  32. * @param string $hostname
  33. * @param integer $port
  34. * @param string $database
  35. * @param string $username
  36. * @param string $password
  37. */
  38. public function initConnection( $hostname , $port , $database , $username , $password )
  39. {
  40. $connection_string = "host={$hostname} port={$port} dbname={$database} user={$username} password={$password}";
  41.  
  42. if( ( $this->connections[] = pg_connect( $connection_string ) ) == true )
  43. {
  44. $connectionId = count($this->connections)-1;
  45.  
  46. $this->activeConnection = $connectionId;
  47.  
  48. return $connectionId;
  49. }
  50. else
  51. {
  52. throw new fw_exception( "Could not connect to the database" );
  53. }
  54. }
  55.  
  56. /**
  57. *
  58. * @access public
  59. * @param integer $key
  60. */
  61. public function setActiveConnection( $key )
  62. {
  63. if( is_numeric( $key ) )
  64. {
  65. $this->activeConnection = $key;
  66. }
  67. else
  68. {
  69. throw new fw_exception( "Key not valid (not an integer)" );
  70. }
  71. }
  72.  
  73. /**
  74. *
  75. * @access public
  76. * @param array $items
  77. * @param string $table
  78. * @param string $condition
  79. */
  80. public function select( $items , $table , $condition = "" )
  81. {
  82. $fields = NULL;
  83.  
  84. if( is_array( $items ) && !empty( $items ) && !empty( $table ) )
  85. {
  86. foreach( $items as $value )
  87. {
  88. $fields .= "{$value},";
  89. }
  90.  
  91. $fields = substr( $fields , 0 , -1 );
  92.  
  93. if( !empty( $condition ) )
  94. {
  95. $condition = "WHERE {$condition}";
  96. }
  97.  
  98. $querystring = "SELECT {$fields} FROM {$table} {$condition}";
  99.  
  100. return $this->executeQuery( $querystring );
  101. }
  102. else
  103. {
  104. throw new fw_exception( "Input data not valid" );
  105. }
  106. }
  107.  
  108. /**
  109. *
  110. * @access public
  111. * @param string $input
  112. */
  113. public function escapeString( $input )
  114. {
  115. return pg_escape_string( $input );
  116. }
  117.  
  118. /**
  119. *
  120. * @access public
  121. * @param integer $key
  122. */
  123. public function numRows( $key )
  124. {
  125. return pg_num_rows( $this->resultset[$key] );
  126. }
  127.  
  128. /**
  129. *
  130. * @access public
  131. * @param integer $key
  132. */
  133. public function fetchArray( $key )
  134. {
  135. return pg_fetch_array( $this->resultset[$key] , NULL , PGSQL_ASSOC );
  136. }
  137.  
  138. /**
  139. *
  140. * @access public
  141. */
  142. public function getDbname()
  143. {
  144. return pg_dbname( $this->connections[$this->activeConnection] );
  145. }
  146.  
  147. /**
  148. *
  149. * @access public
  150. */
  151. public function getPort()
  152. {
  153. return pg_port( $this->connections[$this->activeConnection] );
  154. }
  155.  
  156. /**
  157. *
  158. * @access public
  159. * @param string $table
  160. * @param array $input
  161. */
  162. public function insert( $table , $input )
  163. {
  164. $fields = NULL;
  165. $values = NULL;
  166.  
  167. if( !empty( $table ) && is_array( $input ) && !empty( $input ) )
  168. {
  169. foreach( $input as $key=>$value )
  170. {
  171. $fields .= "{$key},";
  172. $values .= "'".self::escapeString( $value )."',";
  173. }
  174.  
  175. $fields = substr( $fields , 0 , -1 );
  176. $values = substr( $values , 0 , -1 );
  177.  
  178. $querystring = "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
  179.  
  180. return $this->executeQuery( $querystring );
  181.  
  182. }
  183. else
  184. {
  185. throw new fw_exception( "Input data not valid" );
  186. }
  187. }
  188.  
  189. /**
  190. *
  191. * @access public
  192. * @param string $table
  193. * @param array $input
  194. * @param string $condition
  195. */
  196. public function update( $table , $input , $condition )
  197. {
  198. $updates = NULL;
  199.  
  200. if( !empty( $table ) && is_array( $input ) && !empty( $input ) && !empty( $condition ) )
  201. {
  202. foreach( $input as $key=>$value )
  203. {
  204. $updates .= "{$key} = '{$value}',";
  205. }
  206.  
  207. $updates = substr( $updates , 0 , -1 );
  208.  
  209. $querystring = "UPDATE {$table} SET {$updates} WHERE {$condition}";
  210.  
  211. return $this->executeQuery( $querystring );
  212. }
  213. else
  214. {
  215. throw new fw_exception( "Input data not valid" );
  216. }
  217. }
  218.  
  219. /**
  220. *
  221. * @access public
  222. * @param string $table
  223. * @param string $condition
  224. */
  225. public function delete( $table , $condition )
  226. {
  227. if( !empty( $table) && !empty( $condition ) )
  228. {
  229. $querystring = "DELETE FROM {$table} WHERE {$condition}";
  230.  
  231. return $this->executeQuery( $querystring );
  232. }
  233. else
  234. {
  235. throw new fw_exception( "Input data not valid" );
  236. }
  237. }
  238.  
  239. /**
  240. *
  241. * @access public
  242. * @param string $querystring
  243. */
  244. public function executeQuery( $querystring )
  245. {
  246. if( $result = pg_query( $this->connections[$this->activeConnection] , $querystring ) )
  247. {
  248. $this->resultset[] = $result;
  249.  
  250. return count($this->resultset)-1;
  251. }
  252. else
  253. {
  254. throw new fw_exception( "Query failed" );
  255. }
  256. }
  257.  
  258. /**
  259. *
  260. * @access public
  261. * @param integer $key
  262. */
  263. public function affectedRows( $key )
  264. {
  265. return pg_affected_rows( $this->resultset[$key] );
  266. }
  267.  
  268. /**
  269. *
  270. * @access public
  271. * @param integer $key
  272. */
  273. public function closeConnection( $key = NULL )
  274. {
  275. if( is_null( $key ) )
  276. {
  277. return pg_close( $this->connections[$this->activeConnection] );
  278. }
  279. else
  280. {
  281. return pg_close( $this->connections[$key] );
  282. }
  283. }
  284. }
  285. ?>


framework.exc.php
  1. <?php
  2.  
  3. //////////////////////////////////
  4. // EXCEPTIONS/FRAMEWORK.EXC.PHP //
  5. //////////////////////////////////
  6.  
  7. class fw_exception extends exception
  8. {
  9. public function __construct( $message , $code = 0 , Exception $previous = null )
  10. {
  11. parent::__construct( $message , $code , $previous );
  12. }
  13.  
  14. public function __toString()
  15. {
  16. return __CLASS__.": [{$this->code}]: {$this->message} on line {$this->line}";
  17. }
  18. }
  19.  
  20. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.066s