/branches/kaste/framework/lib/AkBaseModel.php

https://github.com/akelos/v1 · PHP · 150 lines · 97 code · 16 blank · 37 comment · 18 complexity · 84cddd9f8e3b84515abc89042fd3b74b MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos Framework - http://www.akelos.org |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
  7. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  8. // +----------------------------------------------------------------------+
  9. /**
  10. * @package ActiveRecord
  11. * @subpackage Base
  12. * @author Bermi Ferrer <bermi a.t akelos c.om>
  13. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  14. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  15. */
  16. defined('AK_LOG_EVENTS') ? null : define('AK_LOG_EVENTS', false);
  17. require_once(AK_LIB_DIR.DS.'Ak.php');
  18. require_once(AK_LIB_DIR.DS.'AkInflector.php');
  19. require_once(AK_LIB_DIR.DS.'AkObject.php');
  20. /**
  21. * This is the base class for all sort of models (Mailers or Active records)
  22. * It handles the naming conventions for models, as in PHP4 all methods appear lowercased
  23. * we to work around to find the real case of the methos to apply conventions.
  24. *
  25. * See also <AkActiveRecord> and <AkActionMailer> as those are the ones you will usually inherit from
  26. *
  27. * @author Bermi Ferrer <bermi a.t akelos c.om>
  28. * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
  29. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  30. */
  31. class AkBaseModel extends AkObject
  32. {
  33. var $_modelName;
  34. /**
  35. * Returns current model name
  36. */
  37. function getModelName()
  38. {
  39. if(!isset($this->_modelName)){
  40. if(!$this->setModelName()){
  41. trigger_error(Ak::t('Unable to fetch current model name'),E_USER_NOTICE);
  42. }
  43. }
  44. return $this->_modelName;
  45. }
  46. /**
  47. * Sets current model name
  48. *
  49. * Use this option if the model name can't be guessed by the Active Record
  50. */
  51. function setModelName($model_name = null)
  52. {
  53. if(!empty($model_name)){
  54. $this->_modelName = $model_name;
  55. }else{
  56. $this->_modelName = $this->_getModelName(get_class($this));
  57. }
  58. return true;
  59. }
  60. /**
  61. * This method will nicely handle model names even on
  62. * PHP where class names are all lowercased
  63. */
  64. function _getModelName($class_name)
  65. {
  66. if(AK_PHP5){
  67. return $class_name;
  68. }
  69. $included_models = $this->_getIncludedModelNames();
  70. if(!in_array($class_name, $included_models)){
  71. $class_name = strtolower($class_name);
  72. foreach ($included_models as $included_model){
  73. if($class_name == strtolower($included_model)){
  74. return $included_model;
  75. }
  76. }
  77. trigger_error(Ak::t('The Akelos Framework could not automatically configure your model name.'.
  78. ' This might be caused because your model file is not located on %path. Please call $this->setModelName("YourModelName");'.
  79. ' in your model constructor in order to make this work.',array('%path'=>AK_MODELS_DIR.DS)), E_USER_ERROR);
  80. return false;
  81. }
  82. }
  83. function getParentModelName()
  84. {
  85. if(!isset($this->_parentModelName)){
  86. if(!$this->setParentModelName()){
  87. return false;
  88. }
  89. }
  90. return $this->_parentModelName;
  91. }
  92. function setParentModelName($model_name = null)
  93. {
  94. $got_errors = false;
  95. if(!empty($model_name)){
  96. $this->_parentModelName = $model_name;
  97. }else{
  98. $class_name = AkInflector::camelize(get_parent_class($this));
  99. if(!AK_PHP5){
  100. $included_models = $this->_getIncludedModelNames();
  101. if(!in_array($class_name, $included_models)){
  102. $class_name = strtolower($class_name);
  103. foreach ($included_models as $included_model){
  104. if($class_name == strtolower($included_model)){
  105. $this->_parentModelName = $included_model;
  106. return true;
  107. }
  108. }
  109. $got_errors = true;
  110. }
  111. }
  112. if($got_errors || $class_name == 'AkActiveRecord'){
  113. trigger_error(Ak::t('The Akelos Framework could not automatically configure your model name.'.
  114. ' This might be caused because your model file is not located on %path. Please call $this->setParentModelName("YourParentModelName");'.
  115. ' in your model constructor in order to make this work.',array('%path'=>AK_MODELS_DIR.DS)), E_USER_ERROR);
  116. return false;
  117. }
  118. $this->_parentModelName = $class_name;
  119. }
  120. return true;
  121. }
  122. function _getIncludedModelNames()
  123. {
  124. $included_files = get_included_files();
  125. $models = array();
  126. foreach ($included_files as $file_name){
  127. if(strstr($file_name,AK_MODELS_DIR)){
  128. $models[] = AkInflector::camelize(str_replace(array(AK_MODELS_DIR.DS,'.php'),'',$file_name));
  129. }
  130. }
  131. return $models;
  132. }
  133. }
  134. ?>