Při vytváření helperu problem s Allowed memory
- t0x1c
- Člen | 151
Zdravím snažím se vytvořit helper abych měl přehlednejší template a
tim pádem tam nemusel mít delší if.
Helper jsem si vytvořil takto
$this->template->registerHelper('rarity', function ($rarity) {
$color;
if (empty($rarity)) {
$color = 'b0c3d9';
} elseif ($rarity == "uncommon") {
$color = '5e98d9';
} elseif ($rarity == "rare") {
$color = '4b69ff';
} elseif ($rarity == "mythical") {
$color = '8847ff';
} elseif ($rarity == "legendary") {
$color = 'd32ce6';
} elseif ($rarity == "acient") {
$color = 'eb4b4b';
} elseif ($rarity == "immortal") {
$color = 'e4ae39';
} else {
$color = 'FFF';
}
return $color;
});
Poté když vypisuju ve foreach tak misto takovehoto for
{if empty($test['items_game']['items'][$item['defindex']]['item_rarity'])}
{var $color = "b0c3d9"}
{elseif $test['items_game']['items'][$item['defindex']]['item_rarity'] == "uncommon"}
{var $color = "5e98d9"}
{elseif $test['items_game']['items'][$item['defindex']]['item_rarity'] == "rare"}
{var $color = "4b69ff"}
{elseif $test['items_game']['items'][$item['defindex']]['item_rarity'] == "mythical"}
{var $color = "8847ff"}
{elseif $test['items_game']['items'][$item['defindex']]['item_rarity'] == "legendary"}
{var $color = "d32ce6"}
{elseif $test['items_game']['items'][$item['defindex']]['item_rarity'] == "acient"}
{var $color = "eb4b4b"}
{elseif $test['items_game']['items'][$item['defindex']]['item_rarity'] == "immortal"}
{var $color = "e4ae39"}
{else}
{/if}
použiju
{$test['items_game']['items'][$item['defindex']]['item_rarity']|rarity}
Ale když toto udělám tak mi to vyhodi hlášku Allowed memory size of 73400320 bytes exhausted (tried to allocate 4883928 bytes). Přitom když mam podminku přímo v template tak je to v pohodě. Helpery jsou náročnější nebo nechápu proč mi to dělá. Příjde mi to jako elegantnější řešení ne?
- t0x1c
- Člen | 151
Tu je celý presnter v podstatě vše je už z načitano z cache. Jedine co tam dělám je že měním klíč v poli to bych eventualně mohl tež zkusit uložit do cache. Zatím si jen tak hraju ale planuju z toho udělat projekt a většinu těch ukonu budu cachovat jednou možná dvakrát denně pomoci cronu. A nebo furt ale na pozadí pomoci javascriptu a handle.
<?php
/**
* Description of testPresenter
*
* @author t0x1c
*/
use Nette\Utils\Arrays;
use Nette\Caching\Cache;
class TestPresenter extends BasePresenter {
private $steam;
private $key;
private $playerItems;
private $allItems;
private $rarities;
private $userInfo;
private $cache;
private $test;
/**
* (non-phpDoc)
*
* @see Nette\Application\Presenter#startup()
*/
public function vdfToJson($url) {
//load json either from API call or fetching from file/url
//no matter your method, $json must contain the data from items_game.txt
$json = file_get_contents($url);
//encapsulate in braces
$json = "{\n$json\n}";
//replace open braces
$pattern = '/"([^"]*)"(\s*){/';
$replace = '"${1}": {';
$json = preg_replace($pattern, $replace, $json);
//replace values
$pattern = '/"([^"]*)"\s*"([^"]*)"/';
$replace = '"${1}": "${2}",';
$json = preg_replace($pattern, $replace, $json);
//remove trailing commas
$pattern = '/,(\s*[}\]])/';
$replace = '${1}';
$json = preg_replace($pattern, $replace, $json);
//add commas
$pattern = '/([}\]])(\s*)("[^"]*":\s*)?([{\[])/';
$replace = '${1},${2}${3}${4}';
$json = preg_replace($pattern, $replace, $json);
//object as value
$pattern = '/}(\s*"[^"]*":)/';
$replace = '},${1}';
return $json = preg_replace($pattern, $replace, $json);
}
protected function startup() {
parent::startup();
$this->key = '4F831C1A3D0D0301BE17C63FC0DA3F7C';
// $this->playerItems = json_decode(file_get_contents('http://api.steampowered.com/IEconItems_570/GetPlayerItems/v0001/?language=en&key=' . $this->key . '&steamid=' . $this->user->getIdentity()->id . ''));
// $this->allItems = json_decode(file_get_contents('http://api.steampowered.com/IEconItems_570/GetSchema/v0001/?language=cs&key=' . $this->key . ''));
// $this->rarities = Nette\Utils\Json::decode(file_get_contents('http://api.steampowered.com/IEconDOTA2_570/GetRarities/v01/?language=cs&key=' . $this->key . '&steamid='.$this->user->getIdentity()->id.''));
// $this->userInfo = json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $this->key . '&steamids=' . $this->user->getIdentity()->id . ''));
// $this->test = json_decode($this->vdfToJson('http://media.steampowered.com/apps/570/scripts/items/items_game.19ebf75f728e7cafc77cb03f3c400e0f028e123c.txt'));
$storage = new Nette\Caching\Storages\FileStorage(WWW_DIR . '/../temp');
$this->cache = new Cache($storage);
// $this->cache->save('allItems', $this->allItems);
// $this->cache->save('playerItems', $this->playerItems->result->items);
// $this->cache->save('rarities', $this->rarities);
// $this->cache->save('test', $this->test);
}
public function actionDefault() {
}
public function renderDefault() {
function objectToArray($d) {
if (is_object($d)) {
$d = get_object_vars($d);
}
if (is_array($d)) {
return array_map(__FUNCTION__, $d);
} else {
return $d;
}
}
$this->allItems = objectToArray($this->cache->load('allItems'));
$this->allItems = $this->allItems['result']['items'];
foreach ($this->allItems as $val) {
$pole[$val['defindex']] = $val;
}
//Stánkovač
$strankovani = new VisualPaginator($this, 'paginator');
$paginator = $strankovani->getPaginator();
$paginator->itemsPerPage = 16;
$paginator->itemCount = count(objectToArray($this->cache->load('playerItems')));
//Itemy hráče + převod na skupinky po 15
$this->template->playerItems = array_chunk(objectToArray($this->cache->load('playerItems')), $paginator->getItemsPerPage());
//Všechny itemy
$this->template->allItems = $pole;
//Rarities
$this->template->rarity = $this->cache->load('rarities');
//Profilové informace
$this->template->userInfo = $this->userInfo;
$this->template->test = objectToArray($this->cache->load('test'));
$this->template->paginator = $paginator;
$this->template->registerHelper('rarity', function ($rarity) {
$color;
if (empty($rarity)) {
$color = 'b0c3d9';
} elseif ($rarity == "uncommon") {
$color = '5e98d9';
} elseif ($rarity == "rare") {
$color = '4b69ff';
} elseif ($rarity == "mythical") {
$color = '8847ff';
} elseif ($rarity == "legendary") {
$color = 'd32ce6';
} elseif ($rarity == "acient") {
$color = 'eb4b4b';
} elseif ($rarity == "immortal") {
$color = 'e4ae39';
} else {
$color = 'FFF';
}
return $color;
});
}
}
- Jan Tvrdík
- Nette guru | 2595
@t0x1c: Zkus ten problém zreprodukovat na jiném stroji (např. na libovolném hostingu) a na sandboxu.