本文整理汇总了PHP中yii\base\Event::trigger方法的典型用法代码示例。如果您正苦于以下问题:PHP Event::trigger方法的具体用法?PHP Event::trigger怎么用?PHP Event::trigger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\Event
的用法示例。
在下文中一共展示了Event::trigger方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parse
/**
* Parse a Less file to CSS
*/
public function parse($src, $dst, $options)
{
$this->auto = isset($options['auto']) ? $options['auto'] : $this->auto;
$variables = $this->variables;
$assetManager = Yii::$app->assetManager;
// Final url of the file being compiled
$assetUrl = substr($dst, strpos($assetManager->basePath, $assetManager->baseUrl));
$variables['published-url'] = '"' . $assetUrl . '"';
// Root for the published folder
$variables['published-base-url'] = '"/' . implode('/', array_slice(explode('/', ltrim($assetUrl, '/')), 0, 2)) . '"';
$less = new \lessc();
$less->setVariables($variables);
// Compressed setting
if ($this->compressed) {
$less->setFormatter('compressed');
}
\Less_Parser::$default_options['compress'] = $this->compressed;
\Less_Parser::$default_options['relativeUrls'] = $this->relativeUrls;
// Send out pre-compile event
$event = new \yii\base\Event();
$this->runtime = ['sourceFile' => $src, 'destinationFile' => $dst, 'compiler' => $less];
$event->sender = $this;
Event::trigger($this, self::EVENT_BEFORE_COMPILE, $event);
try {
if ($this->auto) {
/* @var FileCache $cacheMgr */
$cacheMgr = Yii::createObject('yii\\caching\\FileCache');
$cacheMgr->init();
$cacheId = 'less#' . $dst;
$cache = $cacheMgr->get($cacheId);
if ($cache === false || @filemtime($dst) < @filemtime($src)) {
$cache = $src;
}
$newCache = $less->cachedCompile($cache);
if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
$cacheMgr->set($cacheId, $newCache);
file_put_contents($dst, $newCache['compiled']);
}
} else {
$less->compileFile($src, $dst);
}
// If needed, respect the users configuration
if ($assetManager->fileMode !== null) {
@chmod($dst, $assetManager->fileMode);
}
unset($this->less);
} catch (\Exception $e) {
throw new \Exception(__CLASS__ . ': Failed to compile less file : ' . $e->getMessage() . '.');
}
}
示例2: actionAccount
/**
* Displays page where user can update account settings (username, email or password).
* @return string|\yii\web\Response
*/
public function actionAccount()
{
$profileModel = $this->finder->findProfileById(\Yii::$app->user->identity->getId());
$accountModel = \Yii::createObject(SettingsForm::className());
$this->performAjaxValidation($profileModel);
$this->performAjaxValidation($accountModel);
if ($profileModel->load(\Yii::$app->request->post()) && $accountModel->load(\Yii::$app->request->post())) {
if ($profileModel->validate() && $accountModel->validate() && $profileModel->save() && $accountModel->save()) {
Event::trigger(BaseController::class, BaseController::EVENT_MODEL_UPDATED, new MessageEvent($profileModel));
\Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Your account has been updated'));
return $this->refresh();
}
}
return $this->render('account', ['profileModel' => $profileModel, 'accountModel' => $accountModel]);
}
示例3: fetchItems
private function fetchItems()
{
$items = is_array($this->items) ? $this->items : $this->invoke($this->items);
if (isset($this->empty)) {
$items = ArrayHelper::merge(['' => empty($this->empty) ? Yii::t('gromver.models', 'Select...') : $this->empty], $items);
}
if (isset($this->editable) && !isset($this->multiple) && ($value = $this->getValue()) && !array_key_exists($value, $items)) {
$items[$value] = $value;
}
if ($this->getModel() instanceof ObjectModelInterface) {
preg_match_all('/\\[([a-zA-Z]\\w*)\\]/', Html::getInputName($this->getModel(), $this->getAttribute()), $matches);
$event = new ListItemsEvent(['items' => $items, 'model' => $this->getModel()->getObjectModel(), 'attribute' => end($matches[1])]);
Event::trigger(static::className(), self::EVENT_FETCH_ITEMS, $event);
return $event->items;
}
return $items;
}
示例4: widget
/**
* Creates a widget instance and runs it.
*
* The widget rendering result is returned by this method.
* @param array $config name-value pairs that will be used to initialize the object properties
* @return string the rendering result of the widget.
* @throws \Exception
*/
public static function widget($config = [])
{
if (!isset($config['class'])) {
$config['class'] = get_called_class();
}
Event::trigger(self::className(), self::EVENT_CREATE, new WidgetCreateEvent($config));
ob_start();
ob_implicit_flush(false);
try {
/* @var $widget Widget */
$widget = Yii::createObject($config);
$out = $widget->run();
} catch (\Exception $e) {
// close the output buffer opened above if it has not been closed already
if (ob_get_level() > 0) {
ob_end_clean();
}
throw $e;
}
$widget->trigger(self::EVENT_AFTER_RUN);
return ob_get_clean() . $out;
}
示例5: _createRatingAddEvent
/**
* @param $id
* @param $rating
*/
private function _createRatingAddEvent($id, $rating)
{
$event = new RatingAddEvent();
$event->recordId = $id;
$event->rating = $rating;
$event->userId = \Yii::$app->user->id;
Event::trigger(RatingModule::className(), RatingModule::EVENT_RATING_ADD, $event);
}
示例6: trigger
/**
* 触发事件(对象的触发)
* @param $name
*/
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
//存在该事件名称
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($this->_events[$name] as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
if ($event->handled) {
return;
}
}
}
/**
* 触发该对象的类的事件,如果该类的每个对象都需要绑定该事件,则把该事件绑定到该类上面,当触发该对象的事件时,则会自动触发该对象的类事件,避免每个类都需要进行绑定
*/
Event::trigger($this, $name, $event);
}
示例7: beforeSave
/**
* changed beforeSave as I want to trigger an individual event for this
* @param [type] $insert [description]
* @return boolean [description]
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
Event::trigger(self::className(), self::EVENT_ACTIVITY_UPDATE, new Event(['sender' => $this]));
\Yii::trace('The Event ' . self::EVENT_ACTIVITY_UPDATE . ' should be fired, pls. check!');
$nextDate = new DateTime($this->next_at);
$this->next_at = $nextDate->format('U');
if (is_null($this->next_by)) {
$this->next_by = $this->created_by;
}
return true;
}
return false;
}
示例8: actionClear
/**
* @return array
* @throws NotFoundHttpException
* @throws \Exception
*/
public function actionClear()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$result = [];
$order = $this->loadOrder();
if (null === $order->stage || true === $order->getImmutability(Order::IMMUTABLE_USER)) {
throw new BadRequestHttpException();
}
$products = array_reduce($order->items, function ($res, $item) {
$res[] = ['model' => $item->product, 'quantity' => $item->quantity, 'orderItem' => $item];
return $res;
}, []);
/** @var OrderItem $item */
foreach ($order->items as $item) {
$item->delete();
}
Order::clearStaticOrder();
$order = $this->loadOrder();
$result['success'] = $order->calculate(true);
$event = new CartActionEvent($order, $products);
Event::trigger($this, self::EVENT_ACTION_CLEAR, $event);
$result['additional'] = $event->getEventData();
$result['products'] = $this->productsModelsToArray($products);
return $result;
}
示例9: test
public function test()
{
Event::trigger(TestEvent::className(), self::TEST);
}
示例10: trigger
/**
* Triggers an event.
* This method represents the happening of an event. It invokes
* all attached handlers for the event including class-level handlers.
* @param string $name the event name
* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
*/
public function trigger($name, Event $event = null)
{
// 保证 behaviors 都加载进来了
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
// 构建Event对象,为传入到handler函数中做准备
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($this->_events[$name] as $handler) {
// 给event的data属性赋值
$event->data = $handler[1];
// handler的函数中传入了一个Event对象
call_user_func($handler[0], $event);
// stop further handling if the event is handled
// 事件是否被handle,当handled被设置为true时,执行到这个event的时候,会停止,并忽略剩下的event
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
// 触发class级别的handler
Event::trigger($this, $name, $event);
}
示例11: _createComplaintAddEvent
/**
* @param $recordId
* @param $type
*/
private function _createComplaintAddEvent($recordId, $type)
{
$event = new ComplaintAddEvent();
$event->recordId = $recordId;
$event->type = $type;
$event->userId = \Yii::$app->user->id;
$event->total = Complaint::find()->where(['record_id' => $recordId, 'type' => $type])->count();
Event::trigger(ComplainModule::className(), ComplainModule::EVENT_COMPLAINT_ADD, $event);
}
示例12: _createCommentAddEvent
/**
*
*/
private function _createCommentAddEvent()
{
$event = new CommentAddEvent();
$event->recordId = $this->_model->record_id;
$event->userId = $this->_model->user_id;
Event::trigger(CommentModule::className(), CommentModule::EVENT_COMMENT_ADD, $event);
}
示例13: actionProcessConfigControl
public function actionProcessConfigControl()
{
$request = \Yii::$app->request;
$actionType = $request->post('actionType');
$response = ['isSuccessful' => true];
try {
$group = new ProcessConfig($request->post('groupName'));
if ($group->hasMethod($actionType)) {
$group->{$actionType}();
}
Event::trigger(Supervisor::className(), Supervisor::EVENT_CONFIG_CHANGED);
} catch (SupervisorException $error) {
$response = ['isSuccessful' => false, 'error' => $error->getMessage()];
}
return $response;
}
示例14: trigger
/**
* Triggers an event.
* This method represents the happening of an event. It invokes
* all attached handlers for the event including class-level handlers.
* @param string $name the event name
* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
* @return array|mixed
*/
public function trigger($name, Event $event = null)
{
$return_data = [];
if (!empty($this->_events[$name])) {
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($this->_events[$name] as $handler) {
$event->data = $handler[1];
$return_data[] = call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
break;
}
}
}
// invoke class-level attached handlers
// but won't return anything in this case...
Event::trigger($this, $name, $event);
return $return_data;
}
示例15: actionUpdate
/**
* Updates an existing User model.
* If update is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$user = $this->findModel($id);
$user->scenario = 'update';
$profile = $this->finder->findProfileById($id);
$r = \Yii::$app->request;
$this->performAjaxValidation([$user, $profile]);
if ($user->load($r->post()) && $profile->load($r->post()) && $user->save() && $profile->save()) {
Event::trigger(BaseController::class, BaseController::EVENT_MODEL_UPDATED, new MessageEvent($profile));
$this->trigger(BaseController::EVENT_MODEL_UPDATED, new MessageEvent($user));
\Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been updated'));
return $this->redirect(['index']);
}
return $this->render('update', ['user' => $user, 'profile' => $profile, 'module' => $this->module]);
}