/** * * 從陣列提取 key(s) 做為 index * */ function keyi($array, $key, $gather = false) { if (!$array) { return !is_array($array) ? false : array(); } $ret = array(); $key = (array) $key; reset($array); $_key = array_shift($key); if (isset($array[0])) { if (is_array($array[0])) { while (list(, $v) = each($array)) { !isset($v[$_key]) or !$gather ? !isset($ret[$v[$_key]]) ? $ret[$v[$_key]] = $v : null : ($gather === true ? $ret[$v[$_key]][] = $v : ($ret[$v[$_key]][$gather] = $v)); } } else { if (is_object($array[0])) { while (list(, $v) = each($array)) { !isset($v->{$_key}) or !$gather ? !isset($ret[$v->{$_key}]) ? $ret[$v->{$_key}] = $v : null : ($gather === true ? $ret[$v->{$_key}][] = $v : ($ret[$v->{$_key}][$gather] = $v)); } } } } else { $ret[$array[$_key]] = $array; } if (isset($key[0])) { while (list($k, $r) = each($ret)) { $ret[$k] = keyi($r, $key, $gather); } } return $ret; }
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; /** * * Trigger * */ $app->get('/api/evernote/webhook/gateway', function (Request $request) use($app) { $hookmap = ['notebook_create' => 'notebook/create', 'notebook_update' => 'notebook/update', 'create' => 'note/create', 'update' => 'note/update']; $webhook = $request->query->get('reason'); $identity = $request->query->get('userId'); if (isset($hookmap[$webhook])) { $trigger = $hookmap[$webhook]; $auth = Model::factory('Auth')->where('app_name', 'evernote')->where('app_identity', $identity)->find_array(); if (!empty($auth)) { $auth = keyi($auth, 'id'); $ids = array_keys($auth); $recipe = Model::factory('Recipe')->where_in('trigger_auth_id', $ids)->where('trigger_name', $trigger)->find_array(); $client = []; foreach ($recipe as $r) { if (!isset($auth[$r['trigger_auth_id']])) { continue; } $a = $auth[$r['trigger_auth_id']]; if (!isset($client[$a['id']])) { $client[$a['id']] = new \Evernote\Client($a['app_token'], false); } $c = $client[$a['id']]; $resp = $app->fireoff($app->url("api/evernote/trigger/{$trigger}"), [], ['attributes' => ['recipe' => $r, 'client' => $c, 'auth' => $a]]); } }