📜 ⬆️ ⬇️

Accelerate PHP development with overload

Problem


Often you have to write like this (code examples on Yii, but the approach can be applied to any code):
$model = new User(); $model->name = ''; if (!$model->save()) throw new RuntimeException('Can not save!'); 

Or so:
 $model = User::model()->find(); if (!$model) throw new CHttpException(404, 'User not found!'); 

Caching:
 $dependency = new \caching\TagDependency('Post'); $posts = Post::model()->cache(1000, $dependency)->findAll(); 

Transactions:
 $trx = $this->getDbConnection()->beginTransaction(); try { if (!$user->makePayment()) throw new \RuntimeException('Can not complete!'); $trx->commit(); } catch (\Exception $e) { $trx->rollback(); throw $e; } 


Too much code!

Decision


The above pieces of code can be reduced to:
We throw an exception in case of unsuccessful result:
 $model->saveException(); User::model()->findException(); Post::model()->findAllCached(); 

')
Wrap the method in a transaction:
 $user->makePaymentTrx(); 


Combinations are also possible:
 User::model()->findTrxCached() 




How to implement it?


On Yii, we create our own ActiveRecord class and inherit our models from it:
 class ActiveRecord extends CActiveRecord { public function __call($name, $args) { if (preg_match('/^(.+)(cached|exception|trx)$/i', $name, $matches)) { switch (strtolower($matches[2])) { case 'cached': return $this->cachedMethod($matches[1], $args); case 'exception': return $this->exceptionMethod($matches[1], $args); case 'trx': return $this->trxMethod($matches[1], $args); } } return parent::__call($name, $args); } public function trxMethod($method, $args) { $trx = $this->getDbConnection()->beginTransaction(); try { $value = call_user_func_array(array($this, $method), $args); $trx->commit(); } catch (\Exception $e) { $trx->rollback(); throw $e; } return $value; } public function exceptionMethod($method, $args) { $value = call_user_func_array(array($this, $method), $args); if (!$value) throw new Exception('False result!'); return $value; } public function cachedMethod($method, $args) { $key = get_class($this) . $method . serialize($this->getPrimaryKey()) . serialize($args) . serialize($this->getDbCriteria()); $key = md5($key); $value = \Yii::app()->cache->get($key); if ($value === false) { $value = call_user_func_array(array($this, $method), $args); \Yii::app()->cache->set($key, $value, 0, new \caching\TagDependency(get_class($this))); } else { //reset scope as in find*() methods $this->resetScope(); } return $value; } } 


The basis of the method is a regular expression:
 preg_match('/^(.+)(cached|exception|trx)$/i', $name, $matches) 


You can add your suffixes here.

Benefits:




Disadvantages:


Source: https://habr.com/ru/post/218471/


All Articles