|
| 1 | +# PHP Spring Lib |
| 2 | + |
| 3 | +> Original Repos: |
| 4 | +> - PHP Spring Lib: https://github.com/a19836/phpspringlib/ |
| 5 | +> - Bloxtor: https://github.com/a19836/bloxtor/ |
| 6 | +
|
| 7 | +## Overview |
| 8 | + |
| 9 | +**PHP Spring Lib** is a library that brings core concepts of the Java Spring ecosystem into PHP, using XML-based bean configuration. |
| 10 | +It includes **ODI (Object Dependency Injection)**, **iBatis-style SQL mapping**, and **Hibernate-inspired ORM**, all configured through XML files. |
| 11 | +With this XML-style library, you can create your own XML bean definitions that map directly to PHP classes and other things. |
| 12 | + |
| 13 | +The goal of this library is to provide a PHP architecture similar to Java Spring, enriched with Beans-style ODI, iBatis-style query mappings and Hibernate-like ORM behavior, enabling PHP developers to create decoupled, modular, and scalable applications. |
| 14 | + |
| 15 | +To see a working example, open [index.php](index.php) on your server. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Purpose |
| 20 | + |
| 21 | +This library demonstrates how to recreate: |
| 22 | + |
| 23 | +- **Spring ODI** (dependency injection via XML beans) |
| 24 | +- **iBatis** (SQL mapping through XML) |
| 25 | +- **Hibernate ORM** (object mapping and persistence logic through XML) |
| 26 | + |
| 27 | +in a PHP environment, providing a structured and enterprise-style architecture. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Tutorials |
| 32 | + |
| 33 | +- [ODI – Object Dependency Injection](examples/odi/) |
| 34 | +- [Extending Spring ODI](examples/odi/services.php) |
| 35 | +- [Spring iBatis](examples/ibatis/) |
| 36 | +- [Extending Spring iBatis](examples/ibatis/services.php) |
| 37 | +- [Spring Hibernate](examples/hibernate/) |
| 38 | +- [Extending Spring Hibernate](examples/hibernate/services.php) |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### ODI Usage Example |
| 45 | + |
| 46 | +```php |
| 47 | +//init bean factory |
| 48 | +$BeanFactory = new BeanFactory(); |
| 49 | +$BeanFactory->init(array( |
| 50 | + "file" => __DIR__ . "/examples/odi/assets/beans.xml" |
| 51 | +)); |
| 52 | +$BeanFactory->initObjects(); |
| 53 | + |
| 54 | +//call bean objects |
| 55 | +$BeanObj = $BeanFactory->getObject("MyBeanId"); |
| 56 | + |
| 57 | +//call methods defined in the correspondent classes of the bean object |
| 58 | +$BeanObj->foo(); |
| 59 | +``` |
| 60 | + |
| 61 | +More examples [here](examples/odi/index.php). |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### iBatis Usage Example |
| 66 | + |
| 67 | +```php |
| 68 | +//init DB connection |
| 69 | +$DBBroker = new MySQLDBBroker($host, $username, $password, $db_name); |
| 70 | + |
| 71 | +//init iBatis engine |
| 72 | +$SQLClient = new IBatisClient(); |
| 73 | +$SQLClient->setRDBBroker($DBBroker); |
| 74 | +$SQLClient->loadXML(__DIR__ . "/examples/ibatis/assets/item.xml"); |
| 75 | + |
| 76 | +//call an iBatis query and execute it |
| 77 | +$query = $SQLClient->getQuery("select", "select_item"); |
| 78 | +$result = $SQLClient->execQuery($query, array("item_id" => 1)); |
| 79 | + |
| 80 | +$ItemTest = $result[0]; |
| 81 | +$id = $ItemTest->getId(); |
| 82 | +$title = $ItemTest->getTitle(); |
| 83 | +``` |
| 84 | + |
| 85 | +More examples [here](examples/ibatis/index.php). |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +### Hibernate Usage Example |
| 90 | + |
| 91 | +```php |
| 92 | +//init DB connection |
| 93 | +$DBBroker = new MySQLDBBroker($host, $username, $password, $db_name); |
| 94 | + |
| 95 | +//init Hibernate engine |
| 96 | +$SQLClient = new HibernateClient(); |
| 97 | +$SQLClient->setRDBBroker($DBBroker); |
| 98 | +$SQLClient->loadXML(__DIR__ . "/examples/hibernate/assets/item_subitem.xml"); |
| 99 | + |
| 100 | +//call a Hibernate object |
| 101 | +$ItemObj = $SQLClient->getHbnObj("ItemObj"); |
| 102 | +$result = $ItemObj->findById(1); |
| 103 | +$results = $ItemObj->find(); |
| 104 | +``` |
| 105 | + |
| 106 | +More examples [here](examples/hibernate/index.php). |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## Available Methods |
| 111 | + |
| 112 | +### ODI Methods |
| 113 | + |
| 114 | +```php |
| 115 | +$BeanFactory = new BeanFactory(); |
| 116 | +$BeanFactory->setCacheRootPath( sys_get_temp_dir() . "/cache/spring/" ); //set cache to be faster |
| 117 | + |
| 118 | +$data = array( |
| 119 | + "external_vars" => null, //optional associative array with key-value items |
| 120 | + "file" => "bean.xml", //file path with xml beans |
| 121 | + "settings" => null, //optional array with associative arrays inside, with the following keys: "import", "bean", "var" and "function". |
| 122 | +); |
| 123 | +$BeanFactory->init($data); //init factory based in $data |
| 124 | +$BeanFactory->add($data); //add to factory based in $data |
| 125 | +$BeanFactory->reset(); //clean saved data |
| 126 | + |
| 127 | +$BeanFactory->getSettingsFromFile($file_path); //return settings after parsing xml file |
| 128 | +$BeanFactory->getBeansFromSettings($settings, &$sort_elements = false); //return beans after parsing xml file |
| 129 | + |
| 130 | +$BeanFactory->initObjects(); //init all parsed objects |
| 131 | +$BeanFactory->initObject($bean_name, $launch_exception = true); //init a specific object |
| 132 | +$BeanFactory->initFunction($function, $launch_exception = true); //init a specific function |
| 133 | + |
| 134 | +$BeanFactory->addBeans($beans); //add new beans |
| 135 | +$BeanFactory->getBeans(); //get parsed beans |
| 136 | +$BeanFactory->getBean($bean_name); //get a specific beans |
| 137 | + |
| 138 | +$BeanFactory->addObjects($objs); //add new objects |
| 139 | +$BeanFactory->getObjects(); //get initialized objects |
| 140 | +$BeanFactory->getObject($obj_name); //get a specific initialized object |
| 141 | + |
| 142 | +$BeanFactory->setCacheRootPath($dir_path); //set cache folder path, so next time it does not need to parse the xml file |
| 143 | +$BeanFactory->setCacheHandler(XmlSettingsCacheHandler $XmlSettingsCacheHandler); //set a different cache engine |
| 144 | +``` |
| 145 | + |
| 146 | +### iBatis Methods |
| 147 | + |
| 148 | +```php |
| 149 | +$SQLClient = new IBatisClient(); |
| 150 | +$SQLClient->setRDBBroker($DBBroker); |
| 151 | +$SQLClient->setCacheRootPath( sys_get_temp_dir() . "/cache/spring/ibatis/" ); //set cache to be faster |
| 152 | + |
| 153 | +$SQLClient->loadXML($obj_path, $external_vars = false); |
| 154 | +$query = $SQLClient->getQuery($query_type, $query_id); |
| 155 | +$result = $SQLClient->execQuery($query, $parameters, $options = null); |
| 156 | +$sql = $SQLClient->getQuerySQL($query, $parameters, $options = null); |
| 157 | +$result = $SQLClient->getFunction($function_name, $parameters, $options = null); |
| 158 | +$result = $SQLClient->getData($sql, $options = null); |
| 159 | +$status = $SQLClient->setData($sql, $options = null); |
| 160 | +$result = $SQLClient->getSQL($sql, $options = null); |
| 161 | +$status = $SQLClient->setSQL($sql, $options = null); |
| 162 | +$id = $SQLClient->getInsertedId($options = null); |
| 163 | +$status = $SQLClient->insertObject($table_name, $attributes, $options = null); |
| 164 | +$status = $SQLClient->updateObject($table_name, $attributes, $conditions, $options = null); |
| 165 | +$status = $SQLClient->deleteObject($table_name, $conditions, $options = null); |
| 166 | +$result = $SQLClient->findObjects($table_name, $attributes, $conditions, $options = null); |
| 167 | +$total = $SQLClient->countObjects($table_name, $conditions, $options = null); |
| 168 | +$result = $SQLClient->findRelationshipObjects($table_name, $rel_elm, $parent_conditions, $options = null); |
| 169 | +$total = $SQLClient->countRelationshipObjects($table_name, $rel_elm, $parent_conditions, $options = null); |
| 170 | +$max = $SQLClient->findObjectsColumnMax($table_name, $attribute_name, $options = null); |
| 171 | +``` |
| 172 | + |
| 173 | +### Hibernate Methods |
| 174 | + |
| 175 | +```php |
| 176 | +$SQLClient = new HibernateClient(); |
| 177 | +$SQLClient->setRDBBroker($DBBroker); |
| 178 | +$SQLClient->setCacheLayer($MyHibernateCache); //set cache to be faster |
| 179 | + |
| 180 | +$SQLClient->loadXML($obj_path, $external_vars = false); |
| 181 | +$obj = $SQLClient->getHbnObj($obj_name, $module_id, $service_id, $options = false); |
| 182 | + |
| 183 | +//OBJET METHODS |
| 184 | +$obj->insert($data, &$ids = false, $options = false); |
| 185 | +$obj->insertAll($data, &$statuses = false, &$ids = false, $options = false); |
| 186 | +$obj->update($data, $options = false); |
| 187 | +$obj->updateAll($data, &$statuses = false, $options = false); |
| 188 | +$obj->insertOrUpdate($data, &$ids = false, $options = false); |
| 189 | +$obj->insertOrUpdateAll($data, &$statuses = false, &$ids = false, $options = false); |
| 190 | +$obj->updateByConditions($data, $options = false); |
| 191 | +$obj->updatePrimaryKeys($data, $options = false); |
| 192 | +$obj->delete($data, $options = false); |
| 193 | +$obj->deleteAll($data, &$statuses = false, $options = false); |
| 194 | +$obj->deleteByConditions($data, $options = false); |
| 195 | +$obj->findById($ids, $data = array(), $options = false); |
| 196 | +$obj->find($data = array(), $options = false); |
| 197 | +$obj->count($data = array(), $options = false); |
| 198 | +$obj->findRelationships($parent_ids, $options = false); |
| 199 | +$obj->findRelationship($rel_name, $parent_ids, $options = false); |
| 200 | +$obj->countRelationships($parent_ids, $options = false); |
| 201 | +$obj->countRelationship($rel_name, $parent_ids, $options = false); |
| 202 | + |
| 203 | +//SAME METHODS THAN ON IBATIS |
| 204 | +$obj->callQuerySQL($query_type, $query_id, $parameters = false); |
| 205 | +$obj->callQuery($query_type, $query_id, $parameters = false, $options = false); |
| 206 | + |
| 207 | +$obj->callInsertSQL($query_id, $parameters = false); |
| 208 | +$obj->callInsert($query_id, $parameters = false, $options = false); |
| 209 | + |
| 210 | +$obj->callUpdateSQL($query_id, $parameters = false); |
| 211 | +$obj->callUpdate($query_id, $parameters = false, $options = false); |
| 212 | + |
| 213 | +$obj->callDeleteSQL($query_id, $parameters = false); |
| 214 | +$obj->callDelete($query_id, $parameters = false, $options = false); |
| 215 | + |
| 216 | +$obj->callSelectSQL($query_id, $parameters = false); |
| 217 | +$obj->callSelect($query_id, $parameters = false, $options = false); |
| 218 | + |
| 219 | +$obj->callProcedureSQL($query_id, $parameters = false); |
| 220 | +$obj->callProcedure($query_id, $parameters = false, $options = false); |
| 221 | + |
| 222 | +//SAME METHODS THAN ON DB BROKER |
| 223 | +$obj->getData($sql, $options = false); |
| 224 | +$obj->setData($sql, $options = false); |
| 225 | +$obj->getSQL($sql, $options = false); |
| 226 | +$obj->setSQL($sql, $options = false); |
| 227 | +$obj->getInsertedId($options = false); |
| 228 | +$obj->getFunction($function_name, $parameters = false, $options = false); |
| 229 | + |
| 230 | +//SOME GETTERS METHODS |
| 231 | +$obj->getCacheLayer(); |
| 232 | +$obj->getObjName(); |
| 233 | +$obj->getTableName(); |
| 234 | +$obj->getExtendClassName(); |
| 235 | +$obj->getExtendClassPath(); |
| 236 | +$obj->getIds(); |
| 237 | +$obj->getParameterClass(); |
| 238 | +$obj->getParameterMap(); |
| 239 | +$obj->getResultClass(); |
| 240 | +$obj->getResultMap(); |
| 241 | +$obj->getTableAttributes(); |
| 242 | +$obj->getManyToOne(); |
| 243 | +$obj->getManyToMany(); |
| 244 | +$obj->getOneToMany(); |
| 245 | +$obj->getOneToOne(); |
| 246 | +$obj->getQueries(); |
| 247 | +$obj->getPropertiesToAttributes(); |
| 248 | +$obj->getModuleId(); |
| 249 | +$obj->getServiceId(); |
| 250 | +``` |
| 251 | + |
0 commit comments