Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1347 lines (1167 sloc) 46.125 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
<?php
/**
* Dynamically handles many centralized object-relational mapping tasks
*
* @copyright Copyright (c) 2007-2011 Will Bond
* @author Will Bond [wb] <will@flourishlib.com>
* @author Jeff Turcotte [jt] <jeff.turcotte@gmail.com>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fORM
*
* @version 1.0.0b29
* @changes 1.0.0b29 Added ::registerActiveRecordStaticMethod() for static hooks in PHP 5.3 [jt, 2012-07-25]
* @changes 1.0.0b28 Updated ::getColumnName() and ::getRecordName() to use fText if loaded [wb, 2011-02-02]
* @changes 1.0.0b27 Added links to the detailed documentation for the parameters passed to hooks [wb, 2010-11-27]
* @changes 1.0.0b26 Added ::getRelatedClass() for handling related classes in PHP 5.3 namespaces [wb, 2010-11-17]
* @changes 1.0.0b25 Added support for PHP 5.3 namespaced fActiveRecord classes [wb, 2010-11-11]
* @changes 1.0.0b24 Backwards Compatibility Break - Callbacks registered via ::registerRecordSetMethod() should now accept the `$method_name` in the position where the `$pointer` parameter used to be passed [wb, 2010-09-28]
* @changes 1.0.0b23 Added the `'pre::replicate()'`, `'post::replicate()'` and `'cloned::replicate()'` hooks [wb, 2010-09-07]
* @changes 1.0.0b22 Internal Backwards Compatibility Break - changed ::parseMethod() to not underscorize the subject of the method [wb, 2010-08-06]
* @changes 1.0.0b21 Fixed some documentation to reflect the API changes from v1.0.0b9 [wb, 2010-07-14]
* @changes 1.0.0b20 Added the ability to register a wildcard active record method for all classes [wb, 2010-04-22]
* @changes 1.0.0b19 Added the method ::isClassMappedToTable() [wb, 2010-03-30]
* @changes 1.0.0b18 Added the `post::loadFromIdentityMap()` hook [wb, 2010-03-14]
* @changes 1.0.0b17 Changed ::enableSchemaCaching() to rely on fDatabase::clearCache() instead of explicitly calling fSQLTranslation::clearCache() [wb, 2010-03-09]
* @changes 1.0.0b16 Backwards Compatibility Break - renamed ::addCustomClassToTableMapping() to ::mapClassToTable(). Added ::getDatabaseName() and ::mapClassToDatabase(). Updated code for new fORMDatabase and fORMSchema APIs [wb, 2009-10-28]
* @changes 1.0.0b15 Added support for fActiveRecord to ::getRecordName() [wb, 2009-10-06]
* @changes 1.0.0b14 Updated documentation for ::registerActiveRecordMethod() to include info about prefix method matches [wb, 2009-08-07]
* @changes 1.0.0b13 Updated documentation for ::registerRecordSetMethod() [wb, 2009-07-14]
* @changes 1.0.0b12 Updated ::callReflectCallbacks() to accept a class name instead of an object [wb, 2009-07-13]
* @changes 1.0.0b11 Added ::registerInspectCallback() and ::callInspectCallbacks() [wb, 2009-07-13]
* @changes 1.0.0b10 Fixed a bug with ::objectify() caching during NULL date/time/timestamp values and breaking further objectification [wb, 2009-06-18]
* @changes 1.0.0b9 Added caching for performance and changed some method APIs to only allow class names instead of instances [wb, 2009-06-15]
* @changes 1.0.0b8 Updated documentation to reflect removal of `$associate` parameter for callbacks passed to ::registerRecordSetMethod() [wb, 2009-06-02]
* @changes 1.0.0b7 Added ::enableSchemaCaching() to replace fORMSchema::enableSmartCaching() [wb, 2009-05-04]
* @changes 1.0.0b6 Added the ability to pass a class instance to ::addCustomClassTableMapping() [wb, 2009-02-23]
* @changes 1.0.0b5 Backwards compatibility break - renamed ::addCustomTableClassMapping() to ::addCustomClassTableMapping() and swapped the parameters [wb, 2009-01-26]
* @changes 1.0.0b4 Fixed a bug with retrieving fActiveRecord methods registered for all classes [wb, 2009-01-14]
* @changes 1.0.0b3 Fixed a static method callback constant [wb, 2008-12-17]
* @changes 1.0.0b2 Added ::replicate() and ::registerReplicateCallback() for fActiveRecord::replicate() [wb, 2008-12-12]
* @changes 1.0.0b The initial implementation [wb, 2007-08-04]
*/
class fORM
{
// The following constants allow for nice looking callbacks to static methods
const callHookCallbacks = 'fORM::callHookCallbacks';
const callInspectCallbacks = 'fORM::callInspectCallbacks';
const callReflectCallbacks = 'fORM::callReflectCallbacks';
const checkHookCallback = 'fORM::checkHookCallback';
const classize = 'fORM::classize';
const defineActiveRecordClass = 'fORM::defineActiveRecordClass';
const enableSchemaCaching = 'fORM::enableSchemaCaching';
const getActiveRecordMethod = 'fORM::getActiveRecordMethod';
const getClass = 'fORM::getClass';
const getColumnName = 'fORM::getColumnName';
const getDatabaseName = 'fORM::getDatabaseName';
const getRecordName = 'fORM::getRecordName';
const getRecordSetMethod = 'fORM::getRecordSetMethod';
const getRelatedClass = 'fORM::getRelatedClass';
const isClassMappedToTable = 'fORM::isClassMappedToTable';
const mapClassToDatabase = 'fORM::mapClassToDatabase';
const mapClassToTable = 'fORM::mapClassToTable';
const objectify = 'fORM::objectify';
const overrideColumnName = 'fORM::overrideColumnName';
const overrideRecordName = 'fORM::overrideRecordName';
const parseMethod = 'fORM::parseMethod';
const registerActiveRecordMethod = 'fORM::registerActiveRecordMethod';
const registerActiveRecordStaticMethod = 'fORM::registerActiveRecordStaticMethod';
const registerHookCallback = 'fORM::registerHookCallback';
const registerInspectCallback = 'fORM::registerInspectCallback';
const registerObjectifyCallback = 'fORM::registerObjectifyCallback';
const registerRecordSetMethod = 'fORM::registerRecordSetMethod';
const registerReflectCallback = 'fORM::registerReflectCallback';
const registerReplicateCallback = 'fORM::registerReplicateCallback';
const registerScalarizeCallback = 'fORM::registerScalarizeCallback';
const replicate = 'fORM::replicate';
const reset = 'fORM::reset';
const scalarize = 'fORM::scalarize';
const tablize = 'form::tablize';


/**
* An array of `{method} => {callback}` mappings for fActiveRecord
*
* @var array
*/
static private $active_record_method_callbacks = array();

/**
* An array of `{static_method} => {callback}` mappings for fActiveRecord
*
* @var array
*/
static private $active_record_static_method_callbacks = array();

/**
* Cache for repetitive computation
*
* @var array
*/
static private $cache = array(
'parseMethod' => array(),
'getActiveRecordMethod' => array(),
'objectify' => array()
);

/**
* Custom mappings for class -> database
*
* @var array
*/
static private $class_database_map = array(
'fActiveRecord' => 'default'
);

/**
* Custom mappings for class <-> table
*
* @var array
*/
static private $class_table_map = array();

/**
* Custom column names for columns in fActiveRecord classes
*
* @var array
*/
static private $column_names = array();

/**
* Tracks callbacks registered for various fActiveRecord hooks
*
* @var array
*/
static private $hook_callbacks = array();

/**
* Callbacks for ::callInspectCallbacks()
*
* @var array
*/
static private $inspect_callbacks = array();

/**
* Callbacks for ::objectify()
*
* @var array
*/
static private $objectify_callbacks = array();

/**
* Custom record names for fActiveRecord classes
*
* @var array
*/
static private $record_names = array(
'fActiveRecord' => 'Active Record'
);

/**
* An array of `{method} => {callback}` mappings for fRecordSet
*
* @var array
*/
static private $record_set_method_callbacks = array();

/**
* Callbacks for ::callReflectCallbacks()
*
* @var array
*/
static private $reflect_callbacks = array();

/**
* A cache for resolving related class names for fActiveRecord classes in a PHP 5.3 namespace
*
* @var array
*/
static private $related_class_names = array();

/**
* Callbacks for ::replicate()
*
* @var array
*/
static private $replicate_callbacks = array();

/**
* Callbacks for ::scalarize()
*
* @var array
*/
static private $scalarize_callbacks = array();


/**
* Calls the hook callbacks for the class and hook specified
*
* @internal
*
* @param fActiveRecord $object The instance of the class to call the hook for
* @param string $hook The hook to call
* @param array &$values The current values of the record
* @param array &$old_values The old values of the record
* @param array &$related_records Records related to the current record
* @param array &$cache The cache array of the record
* @param mixed &$parameter The parameter to send the callback
* @return void
*/
static public function callHookCallbacks($object, $hook, &$values, &$old_values, &$related_records, &$cache, &$parameter=NULL)
{
$class = get_class($object);

if (empty(self::$hook_callbacks[$class][$hook]) && empty(self::$hook_callbacks['*'][$hook])) {
return;
}

// Get all of the callbacks for this hook, both for this class or all classes
$callbacks = array();

if (isset(self::$hook_callbacks[$class][$hook])) {
$callbacks = array_merge($callbacks, self::$hook_callbacks[$class][$hook]);
}

if (isset(self::$hook_callbacks['*'][$hook])) {
$callbacks = array_merge($callbacks, self::$hook_callbacks['*'][$hook]);
}

foreach ($callbacks as $callback) {
call_user_func_array(
$callback,
// This is the only way to pass by reference
array(
$object,
&$values,
&$old_values,
&$related_records,
&$cache,
&$parameter
)
);
}
}


/**
* Calls all inspect callbacks for the class and column specified
*
* @internal
*
* @param string $class The class to inspect the column of
* @param string $column The column to inspect
* @param array &$metadata The associative array of data about the column
* @return void
*/
static public function callInspectCallbacks($class, $column, &$metadata)
{
if (!isset(self::$inspect_callbacks[$class][$column])) {
return;
}

foreach (self::$inspect_callbacks[$class][$column] as $callback) {
// This is the only way to pass by reference
$parameters = array(
$class,
$column,
&$metadata
);
call_user_func_array($callback, $parameters);
}
}


/**
* Calls all reflect callbacks for the class passed
*
* @internal
*
* @param string $class The class to call the callbacks for
* @param array &$signatures The associative array of `{method_name} => {signature}`
* @param boolean $include_doc_comments If the doc comments should be included in the signature
* @return void
*/
static public function callReflectCallbacks($class, &$signatures, $include_doc_comments)
{
if (!isset(self::$reflect_callbacks[$class]) && !isset(self::$reflect_callbacks['*'])) {
return;
}

if (!empty(self::$reflect_callbacks['*'])) {
foreach (self::$reflect_callbacks['*'] as $callback) {
// This is the only way to pass by reference
$parameters = array(
$class,
&$signatures,
$include_doc_comments
);
call_user_func_array($callback, $parameters);
}
}

if (!empty(self::$reflect_callbacks[$class])) {
foreach (self::$reflect_callbacks[$class] as $callback) {
// This is the only way to pass by reference
$parameters = array(
$class,
&$signatures,
$include_doc_comments
);
call_user_func_array($callback, $parameters);
}
}
}


/**
* Checks to see if any (or a specific) callback has been registered for a specific hook
*
* @internal
*
* @param string $class The name of the class
* @param string $hook The hook to check
* @param array $callback The specific callback to check for
* @return boolean If the specified callback exists
*/
static public function checkHookCallback($class, $hook, $callback=NULL)
{
if (empty(self::$hook_callbacks[$class][$hook]) && empty(self::$hook_callbacks['*'][$hook])) {
return FALSE;
}

if (!$callback) {
return TRUE;
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

if (!empty(self::$hook_callbacks[$class][$hook]) && in_array($callback, self::$hook_callbacks[$class][$hook])) {
return TRUE;
}

if (!empty(self::$hook_callbacks['*'][$hook]) && in_array($callback, self::$hook_callbacks['*'][$hook])) {
return TRUE;
}

return FALSE;
}


/**
* Takes a table and turns it into a class name - uses custom mapping if set
*
* @param string $table The table name
* @return string The class name
*/
static public function classize($table)
{
if (!$class = array_search($table, self::$class_table_map)) {
$class = fGrammar::camelize(fGrammar::singularize($table), TRUE);
self::$class_table_map[$class] = $table;
}

return $class;
}


/**
* Will dynamically create an fActiveRecord-based class for a database table
*
* Normally this would be called from an `__autoload()` function.
*
* This method will only create classes for tables in the default ORM
* database.
*
* @param string $class The name of the class to create
* @return void
*/
static public function defineActiveRecordClass($class)
{
if (class_exists($class, FALSE)) {
return;
}
$schema = fORMSchema::retrieve();
$tables = $schema->getTables();
$table = self::tablize($class);
if (in_array($table, $tables)) {
eval('class ' . $class . ' extends fActiveRecord { };');
return;
}

throw new fProgrammerException(
'The class specified, %s, does not correspond to a database table',
$class
);
}


/**
* Enables caching on the fDatabase, fSQLTranslation and fSchema objects used for the ORM
*
* This method will cache database schema information to the three objects
* that use it during normal ORM operation: fDatabase, fSQLTranslation and
* fSchema. To allow for schema changes without having to manually clear
* the cache, all cached information will be cleared if any
* fUnexpectedException objects are thrown.
*
* This method should be called right after fORMDatabase::attach().
*
* @param fCache $cache The object to cache schema information to
* @param string $database_name The database to enable caching for
* @param string $key_token This is a token that is used in cache keys to prevent conflicts for server-wide caches - when non-NULL the document root is used
* @return void
*/
static public function enableSchemaCaching($cache, $database_name='default', $key_token=NULL)
{
if ($key_token === NULL) {
$key_token = $_SERVER['DOCUMENT_ROOT'];
}
$token = 'fORM::' . $database_name . '::' . $key_token . '::';

$db = fORMDatabase::retrieve('name:' . $database_name);
$db->enableCaching($cache, $token);
fException::registerCallback($db->clearCache, 'fUnexpectedException');

$sql_translation = $db->getSQLTranslation();
$sql_translation->enableCaching($cache, $token);

$schema = fORMSchema::retrieve('name:' . $database_name);
$schema->enableCaching($cache, $token);
fException::registerCallback($schema->clearCache, 'fUnexpectedException');
}


/**
* Returns a matching callback for the class and method specified
*
* The callback returned will be determined by the following logic:
*
* 1. If an exact callback has been defined for the method, it will be returned
* 2. If a callback in the form `{prefix}*` has been defined that matches the method, it will be returned
* 3. `NULL` will be returned
*
* @internal
*
* @param string $class The name of the class
* @param string $method The method to get the callback for
* @return string|null The callback for the method or `NULL` if none exists - see method description for details
*/
static public function getActiveRecordMethod($class, $method)
{
// This caches method lookups, providing a significant performance
// boost to pages with lots of method calls that get passed to
// fActiveRecord::__call()
if (isset(self::$cache['getActiveRecordMethod'][$class . '::' . $method])) {
return (!$method = self::$cache['getActiveRecordMethod'][$class . '::' . $method]) ? NULL : $method;
}

$callback = NULL;

if (isset(self::$active_record_method_callbacks[$class][$method])) {
$callback = self::$active_record_method_callbacks[$class][$method];

} elseif (isset(self::$active_record_method_callbacks['*'][$method])) {
$callback = self::$active_record_method_callbacks['*'][$method];

} elseif (preg_match('#[A-Z0-9]#', $method)) {
list($action, $subject) = self::parseMethod($method);
if (isset(self::$active_record_method_callbacks[$class][$action . '*'])) {
$callback = self::$active_record_method_callbacks[$class][$action . '*'];
} elseif (isset(self::$active_record_method_callbacks['*'][$action . '*'])) {
$callback = self::$active_record_method_callbacks['*'][$action . '*'];
}
}

self::$cache['getActiveRecordMethod'][$class . '::' . $method] = ($callback === NULL) ? FALSE : $callback;
return $callback;
}

/**
* Returns a matching callback for the class and static method specified
*
* The callback returned will be determined by the following logic:
*
* 1. If an exact callback has been defined for the method, it will be returned
* 2. If a callback in the form `{prefix}*` has been defined that matches the method, it will be returned
* 3. `NULL` will be returned
*
* @internal
*
* @param string $class The name of the class
* @param string $method The method to get the callback for
* @return string|null The callback for the method or `NULL` if none exists - see method description for details
*/
static public function getActiveRecordStaticMethod($class, $method)
{
// This caches method lookups, providing a significant performance
// boost to pages with lots of method calls that get passed to
// fActiveRecord::__callStatic()
if (isset(self::$cache['getActiveRecordStaticMethod'][$class . '::' . $method])) {
return (!$method = self::$cache['getActiveRecordStaticMethod'][$class . '::' . $method]) ? NULL : $method;
}

$callback = NULL;

if (isset(self::$active_record_static_method_callbacks[$class][$method])) {
$callback = self::$active_record_static_method_callbacks[$class][$method];

} elseif (isset(self::$active_record_static_method_callbacks['*'][$method])) {
$callback = self::$active_record_static_method_callbacks['*'][$method];

} elseif (preg_match('#[A-Z0-9]#', $method)) {
list($action, $subject) = self::parseMethod($method);
if (isset(self::$active_record_static_method_callbacks[$class][$action . '*'])) {
$callback = self::$active_record_static_method_callbacks[$class][$action . '*'];
} elseif (isset(self::$active_record_static_method_callbacks['*'][$action . '*'])) {
$callback = self::$active_record_static_method_callbacks['*'][$action . '*'];
}
}

self::$cache['getActiveRecordStaticMethod'][$class . '::' . $method] = ($callback === NULL) ? FALSE : $callback;
return $callback;
}


/**
* Takes a class name or class and returns the class name
*
* @internal
*
* @param mixed $class The object to get the name of, or possibly a string already containing the class
* @return string The class name
*/
static public function getClass($class)
{
if (is_object($class)) { return get_class($class); }
return $class;
}


/**
* Returns the column name
*
* The default column name is the result of calling fGrammar::humanize()
* on the column.
*
* @internal
*
* @param string $class The class name the column is part of
* @param string $column The database column
* @return string The column name for the column specified
*/
static public function getColumnName($class, $column)
{
if (!isset(self::$column_names[$class])) {
self::$column_names[$class] = array();
}

if (!isset(self::$column_names[$class][$column])) {
self::$column_names[$class][$column] = fGrammar::humanize($column);
}

// If fText is loaded, use it
if (class_exists('fText', FALSE)) {
return call_user_func(
array('fText', 'compose'),
str_replace('%', '%%', self::$column_names[$class][$column])
);
}

return self::$column_names[$class][$column];
}


/**
* Returns the name for the database used by the class specified
*
* @internal
*
* @param string $class The class name to get the database name for
* @return string The name of the database to use
*/
static public function getDatabaseName($class)
{
if (!isset(self::$class_database_map[$class])) {
$class = 'fActiveRecord';
}

return self::$class_database_map[$class];
}


/**
* Returns the record name for a class
*
* The default record name is the result of calling fGrammar::humanize()
* on the class.
*
* @internal
*
* @param string $class The class name to get the record name of
* @return string The record name for the class specified
*/
static public function getRecordName($class)
{
if (!isset(self::$record_names[$class])) {
self::$record_names[$class] = fGrammar::humanize(
// Strip the namespace off the class name
preg_replace(
'#^.*\\\\#',
'',
$class
)
);
}

// If fText is loaded, use it
if (class_exists('fText', FALSE)) {
return call_user_func(
array('fText', 'compose'),
str_replace('%', '%%', self::$record_names[$class])
);
}

return self::$record_names[$class];
}


/**
* Returns a matching callback for the method specified
*
* The callback returned will be determined by the following logic:
*
* 1. If an exact callback has been defined for the method, it will be returned
* 2. If a callback in the form `{action}*` has been defined that matches the method, it will be returned
* 3. `NULL` will be returned
*
* @internal
*
* @param string $method The method to get the callback for
* @return string|null The callback for the method or `NULL` if none exists - see method description for details
*/
static public function getRecordSetMethod($method)
{
if (isset(self::$record_set_method_callbacks[$method])) {
return self::$record_set_method_callbacks[$method];
}

if (preg_match('#[A-Z0-9]#', $method)) {
list($action, $subject) = self::parseMethod($method);
if (isset(self::$record_set_method_callbacks[$action . '*'])) {
return self::$record_set_method_callbacks[$action . '*'];
}
}

return NULL;
}


/**
* Takes a class name and related class name and ensures the related class has the appropriate namespace prefix
*
* @internal
*
* @param string $class The primary class
* @param string $related_class The related class name
* @return string The related class name, with the appropriate namespace prefix
*/
static public function getRelatedClass($class, $related_class)
{
if (isset(self::$related_class_names[$class][$related_class])) {
return self::$related_class_names[$class][$related_class];
}

$original_related_class = $related_class;
if (strpos($class, '\\') !== FALSE && strpos($related_class, '\\') === FALSE) {
$reflection = new ReflectionClass($class);
$related_class = $reflection->getNamespaceName() . '\\' . $related_class;
}
self::$related_class_names[$class][$original_related_class] = $related_class;

return $related_class;
}


/**
* Checks if a class has been mapped to a table
*
* @internal
*
* @param mixed $class The name of the class
* @return boolean If the class has been mapped to a table
*/
static public function isClassMappedToTable($class)
{
$class = self::getClass($class);

return isset(self::$class_table_map[$class]);
}


/**
* Sets a class to use a database other than the "default"
*
* Multiple database objects can be attached for the ORM by passing a
* unique `$name` to the ::attach() method.
*
* @param mixed $class The name of the class, or an instance of it
* @param string $database_name The name given to the database when passed to ::attach()
* @return void
*/
static public function mapClassToDatabase($class, $database_name)
{
$class = fORM::getClass($class);

self::$class_database_map[$class] = $database_name;
}


/**
* Allows non-standard class to table mapping
*
* By default, all database tables are assumed to be plural nouns in
* `underscore_notation` and all class names are assumed to be singular
* nouns in `UpperCamelCase`. This method allows arbitrary class to
* table mapping.
*
* @param mixed $class The name of the class, or an instance of it
* @param string $table The name of the database table
* @return void
*/
static public function mapClassToTable($class, $table)
{
$class = self::getClass($class);

self::$class_table_map[$class] = $table;
}


/**
* Takes a scalar value and turns it into an object if applicable
*
* @internal
*
* @param string $class The class name of the class the column is part of
* @param string $column The database column
* @param mixed $value The value to possibly objectify
* @return mixed The scalar or object version of the value, depending on the column type and column options
*/
static public function objectify($class, $column, $value)
{
// This short-circuits computation for already checked columns, providing
// a nice little performance boost to pages with lots of records
if (isset(self::$cache['objectify'][$class . '::' . $column])) {
return $value;
}

if (!empty(self::$objectify_callbacks[$class][$column])) {
return call_user_func(self::$objectify_callbacks[$class][$column], $class, $column, $value);
}

$table = self::tablize($class);
$schema = fORMSchema::retrieve($class);

// Turn date/time values into objects
$column_type = $schema->getColumnInfo($table, $column, 'type');

if (in_array($column_type, array('date', 'time', 'timestamp'))) {

if ($value === NULL) {
return $value;
}

try {

// Explicit calls to the constructors are used for dependency detection
switch ($column_type) {
case 'date': $value = new fDate($value); break;
case 'time': $value = new fTime($value); break;
case 'timestamp': $value = new fTimestamp($value); break;
}

} catch (fValidationException $e) {
// Validation exception results in the raw value being saved
}

} else {
self::$cache['objectify'][$class . '::' . $column] = TRUE;
}

return $value;
}


/**
* Allows overriding of default column names
*
* By default a column name is the result of fGrammar::humanize() called
* on the column.
*
* @param mixed $class The class name or instance of the class the column is located in
* @param string $column The database column
* @param string $column_name The name for the column
* @return void
*/
static public function overrideColumnName($class, $column, $column_name)
{
$class = self::getClass($class);

if (!isset(self::$column_names[$class])) {
self::$column_names[$class] = array();
}

self::$column_names[$class][$column] = $column_name;
}


/**
* Allows overriding of default record names
*
* By default a record name is the result of fGrammar::humanize() called
* on the class.
*
* @param mixed $class The class name or instance of the class to override the name of
* @param string $record_name The human version of the record
* @return void
*/
static public function overrideRecordName($class, $record_name)
{
$class = self::getClass($class);
self::$record_names[$class] = $record_name;
}


/**
* Parses a `camelCase` method name for an action and subject in the form `actionSubject()`
*
* @internal
*
* @param string $method The method name to parse
* @return array An array of `0 => {action}, 1 => {subject}`
*/
static public function parseMethod($method)
{
if (isset(self::$cache['parseMethod'][$method])) {
return self::$cache['parseMethod'][$method];
}

if (!preg_match('#^([a-z]+)(.*)$#D', $method, $matches)) {
throw new fProgrammerException(
'Invalid method, %s(), called',
$method
);
}
self::$cache['parseMethod'][$method] = array($matches[1], $matches[2]);
return self::$cache['parseMethod'][$method];
}


/**
* Registers a callback for an fActiveRecord method that falls through to fActiveRecord::__call() or hits a predefined method hook
*
* The callback should accept the following parameters:
*
* - **`$object`**: The fActiveRecord instance
* - **`&$values`**: The values array for the record
* - **`&$old_values`**: The old values array for the record
* - **`&$related_records`**: The related records array for the record
* - **`&$cache`**: The cache array for the record
* - **`$method_name`**: The method that was called
* - **`&$parameters`**: The parameters passed to the method
*
* @param mixed $class The class name or instance of the class to register for, `'*'` will register for all classes
* @param string $method The method to hook for - this can be a complete method name or `{prefix}*` where `*` will match any column name
* @param callback $callback The callback to execute - see method description for parameter list
* @return void
*/
static public function registerActiveRecordMethod($class, $method, $callback)
{
$class = self::getClass($class);

if (!isset(self::$active_record_method_callbacks[$class])) {
self::$active_record_method_callbacks[$class] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$active_record_method_callbacks[$class][$method] = $callback;

self::$cache['getActiveRecordMethod'] = array();
}


/**
* Registers a callback for an fActiveRecord method that falls through to fActiveRecord::__callStatic() or hits a predefined method hook
*
* Only available to PHP 5.3+ which supports the __callStatic magic method.
*
* The callback should accept the following parameters:
*
* - **`&$class`**: The class calling the static method
* - **`$method_name`**: The method that was called
* - **`&$parameters`**: The parameters passed to the method
*
* @throws fProgrammerException When the PHP version less than 5.3
*
* @param mixed $class The class name or instance of the class to register for, `'*'` will register for all classes
* @param string $method The method to hook for - this can be a complete method name or `{prefix}*` where `*` will match any column name
* @param callback $callback The callback to execute - see method description for parameter list
* @return void
*/
static public function registerActiveRecordStaticMethod($class, $method, $callback)
{
if (!fCore::checkVersion('5.3')) {
throw new fProgrammerException(
'fORM::registerActiveRecordStaticMethod is only available to PHP 5.3+',
$method
);
};

$class = self::getClass($class);

if (!isset(self::$active_record_static_method_callbacks[$class])) {
self::$active_record_static_method_callbacks[$class] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$active_record_static_method_callbacks[$class][$method] = $callback;
self::$cache['getActiveRecordStaticMethod'] = array();
}


/**
* Registers a callback for one of the various fActiveRecord hooks - multiple callbacks can be registered for each hook
*
* The method signature should include the follow parameters:
*
* - **`$object`**: The fActiveRecord instance
* - **`&$values`**: The values array for the record - see the [http://flourishlib.com/docs/fORM#values $values] documentation for details
* - **`&$old_values`**: The old values array for the record - see the [http://flourishlib.com/docs/fORM#old_values $old_values] documentation for details
* - **`&$related_records`**: The related records array for the record - see the [http://flourishlib.com/docs/fORM#related_records $related_records] documentation for details
* - **`&$cache`**: The cache array for the record - see the [http://flourishlib.com/docs/fORM#cache $cache] documentation for details
*
* The `'pre::validate()'` and `'post::validate()'` hooks have an extra
* parameter:
*
* - **`&$validation_messages`**: An ordered array of validation errors that will be returned or tossed as an fValidationException - see the [http://flourishlib.com/docs/fORM#validation_messages $validation_messages] documentation for details
*
* The `'pre::replicate()'`, `'post::replicate()'` and
* `'cloned::replicate()'` hooks have an extra parameter:
*
* - **`$replication_level`**: An integer representing the level of recursion - the object being replicated will be `0`, children will be `1`, grandchildren `2` and so on.
*
* Below is a list of all valid hooks:
*
* - `'post::__construct()'`
* - `'pre::delete()'`
* - `'post-begin::delete()'`
* - `'pre-commit::delete()'`
* - `'post-commit::delete()'`
* - `'post-rollback::delete()'`
* - `'post::delete()'`
* - `'post::loadFromIdentityMap()'`
* - `'post::loadFromResult()'`
* - `'pre::populate()'`
* - `'post::populate()'`
* - `'pre::replicate()'`
* - `'post::replicate()'`
* - `'cloned::replicate()'`
* - `'pre::store()'`
* - `'post-begin::store()'`
* - `'post-validate::store()'`
* - `'pre-commit::store()'`
* - `'post-commit::store()'`
* - `'post-rollback::store()'`
* - `'post::store()'`
* - `'pre::validate()'`
* - `'post::validate()'`
*
* @param mixed $class The class name or instance of the class to hook, `'*'` will hook all classes
* @param string $hook The hook to register for
* @param callback $callback The callback to register - see the method description for details about the method signature
* @return void
*/
static public function registerHookCallback($class, $hook, $callback)
{
$class = self::getClass($class);

static $valid_hooks = array(
'post::__construct()',
'pre::delete()',
'post-begin::delete()',
'pre-commit::delete()',
'post-commit::delete()',
'post-rollback::delete()',
'post::delete()',
'post::loadFromIdentityMap()',
'post::loadFromResult()',
'pre::populate()',
'post::populate()',
'pre::replicate()',
'post::replicate()',
'cloned::replicate()',
'pre::store()',
'post-begin::store()',
'post-validate::store()',
'pre-commit::store()',
'post-commit::store()',
'post-rollback::store()',
'post::store()',
'pre::validate()',
'post::validate()'
);

if (!in_array($hook, $valid_hooks)) {
throw new fProgrammerException(
'The hook specified, %1$s, should be one of: %2$s.',
$hook,
join(', ', $valid_hooks)
);
}

if (!isset(self::$hook_callbacks[$class])) {
self::$hook_callbacks[$class] = array();
}

if (!isset(self::$hook_callbacks[$class][$hook])) {
self::$hook_callbacks[$class][$hook] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$hook_callbacks[$class][$hook][] = $callback;
}


/**
* Registers a callback to modify the results of fActiveRecord::inspect() methods
*
* @param mixed $class The class name or instance of the class to register for
* @param string $column The column to register for
* @param callback $callback The callback to register. Callback should accept a single parameter by reference, an associative array of the various metadata about a column.
* @return void
*/
static public function registerInspectCallback($class, $column, $callback)
{
$class = self::getClass($class);

if (!isset(self::$inspect_callbacks[$class])) {
self::$inspect_callbacks[$class] = array();
}
if (!isset(self::$inspect_callbacks[$class][$column])) {
self::$inspect_callbacks[$class][$column] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$inspect_callbacks[$class][$column][] = $callback;
}


/**
* Registers a callback for when ::objectify() is called on a specific column
*
* @param mixed $class The class name or instance of the class to register for
* @param string $column The column to register for
* @param callback $callback The callback to register. Callback should accept a single parameter, the value to objectify and should return the objectified value.
* @return void
*/
static public function registerObjectifyCallback($class, $column, $callback)
{
$class = self::getClass($class);

if (!isset(self::$objectify_callbacks[$class])) {
self::$objectify_callbacks[$class] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$objectify_callbacks[$class][$column] = $callback;

self::$cache['objectify'] = array();
}


/**
* Registers a callback for an fRecordSet method that fall through to fRecordSet::__call()
*
* The callback should accept the following parameters:
*
* - **`$object`**: The actual record set
* - **`$class`**: The class of each record
* - **`&$records`**: The ordered array of fActiveRecord objects
* - **`$method_name`**: The method name that was called
* - **`$parameters`**: Any parameters passed to the method
*
* @param string $method The method to hook for
* @param callback $callback The callback to execute - see method description for parameter list
* @return void
*/
static public function registerRecordSetMethod($method, $callback)
{
if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}
self::$record_set_method_callbacks[$method] = $callback;
}


/**
* Registers a callback to modify the results of fActiveRecord::reflect()
*
* Callbacks registered here can override default method signatures and add
* method signatures, however any methods that are defined in the actual class
* will override these signatures.
*
* The callback should accept three parameters:
*
* - **`$class`**: the class name
* - **`&$signatures`**: an associative array of `{method_name} => {signature}`
* - **`$include_doc_comments`**: a boolean indicating if the signature should include the doc comment for the method, or just the signature
*
* @param mixed $class The class name or instance of the class to register for, `'*'` will register for all classes
* @param callback $callback The callback to register. Callback should accept a three parameters - see method description for details.
* @return void
*/
static public function registerReflectCallback($class, $callback)
{
$class = self::getClass($class);

if (!isset(self::$reflect_callbacks[$class])) {
self::$reflect_callbacks[$class] = array();
} elseif (in_array($callback, self::$reflect_callbacks[$class])) {
return;
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$reflect_callbacks[$class][] = $callback;
}


/**
* Registers a callback for when a value is replicated for a specific column
*
* @param mixed $class The class name or instance of the class to register for
* @param string $column The column to register for
* @param callback $callback The callback to register. Callback should accept a single parameter, the value to replicate and should return the replicated value.
* @return void
*/
static public function registerReplicateCallback($class, $column, $callback)
{
$class = self::getClass($class);

if (!isset(self::$replicate_callbacks[$class])) {
self::$replicate_callbacks[$class] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$replicate_callbacks[$class][$column] = $callback;
}


/**
* Registers a callback for when ::scalarize() is called on a specific column
*
* @param mixed $class The class name or instance of the class to register for
* @param string $column The column to register for
* @param callback $callback The callback to register. Callback should accept a single parameter, the value to scalarize and should return the scalarized value.
* @return void
*/
static public function registerScalarizeCallback($class, $column, $callback)
{
$class = self::getClass($class);

if (!isset(self::$scalarize_callbacks[$class])) {
self::$scalarize_callbacks[$class] = array();
}

if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

self::$scalarize_callbacks[$class][$column] = $callback;
}


/**
* Takes and value and returns a copy is scalar or a clone if an object
*
* The ::registerReplicateCallback() allows for custom replication code
*
* @internal
*
* @param string $class The class the column is part of
* @param string $column The database column
* @param mixed $value The value to copy/clone
* @return mixed The copied/cloned value
*/
static public function replicate($class, $column, $value)
{
if (!empty(self::$replicate_callbacks[$class][$column])) {
return call_user_func(self::$replicate_callbacks[$class][$column], $class, $column, $value);
}

if (!is_object($value)) {
return $value;
}

return clone $value;
}


/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$active_record_method_callbacks = array();
self::$cache = array(
'parseMethod' => array(),
'getActiveRecordMethod' => array(),
'objectify' => array()
);
self::$class_database_map = array(
'fActiveRecord' => 'default'
);
self::$class_table_map = array();
self::$column_names = array();
self::$hook_callbacks = array();
self::$inspect_callbacks = array();
self::$objectify_callbacks = array();
self::$record_names = array(
'fActiveRecord' => 'Active Record'
);
self::$record_set_method_callbacks = array();
self::$reflect_callbacks = array();
self::$related_class_names = array();
self::$replicate_callbacks = array();
self::$scalarize_callbacks = array();
}


/**
* If the value passed is an object, calls `__toString()` on it
*
* @internal
*
* @param mixed $class The class name or instance of the class the column is part of
* @param string $column The database column
* @param mixed $value The value to get the scalar value of
* @return mixed The scalar value of the value
*/
static public function scalarize($class, $column, $value)
{
$class = self::getClass($class);

if (!empty(self::$scalarize_callbacks[$class][$column])) {
return call_user_func(self::$scalarize_callbacks[$class][$column], $class, $column, $value);
}

if (is_object($value) && is_callable(array($value, '__toString'))) {
return $value->__toString();
} elseif (is_object($value)) {
return (string) $value;
}

return $value;
}


/**
* Takes a class name (or class) and turns it into a table name - Uses custom mapping if set
*
* @param string $class The class name
* @return string The table name
*/
static public function tablize($class)
{
if (!isset(self::$class_table_map[$class])) {
self::$class_table_map[$class] = fGrammar::underscorize(fGrammar::pluralize(
// Strip the namespace off the class name
preg_replace(
'#^.*\\\\#',
'',
$class
)
));
}
return self::$class_table_map[$class];
}


/**
* Forces use as a static class
*
* @return fORM
*/
private function __construct() { }
}



/**
* Copyright (c) 2007-2011 Will Bond <will@flourishlib.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Something went wrong with that request. Please try again.