/** * @param $block * @return Block */ public function getBlock($block) { if (isset($this[$block])) { return $this[$block]; } $blockArray = array(); if ($this->getAllowCache()) { $file = $this->cachePath . DIRECTORY_SEPARATOR . $block . '.array'; if (is_file($file)) { $blockArray = (require $file); $block = new Block($blockArray); $this[$block->getName()] = $block; return $block; } } if (empty($blockArray)) { $blockArray = $this->loadBlockArray($block); $block = $this->createBlock($blockArray); $this[$block->getName()] = $block; if ($this->allowCache && isset($file)) { $code = "<?php\n\n return " . var_export($block->toArray(), true) . ";\n"; file_put_contents($file, $code); } } return $this->createBlock($blockArray); }
/** * @param mixed $block * @throws \App\Exception\InvalidArgumentException * @return $this */ public function addBlock($block) { if (is_array($block)) { $block = new Block($block); } elseif (!$block instanceof Block) { throw new InvalidArgumentException('Block must be instance of Block or an array'); } $this[$block->getName()] = $block; $this->isDirtyIndex = true; return $this; }