<commit_before><?php

namespace Carrooi\ImagesManager\Latte;

use Carrooi\ImagesManager\Helpers as ImagesHelpers;
use Carrooi\ImagesManager\ImageNotExistsException;
use Carrooi\ImagesManager\ImagesManager;
use Latte\Macros\MacroSet;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\PhpWriter;

/**
 *
 * @author <NAME>
 */
class Macros extends MacroSet
{


	/**
	 * @param \Latte\Compiler $compiler
	 */
	public static function install(Compiler $compiler)
	{
		$me = new static($compiler);		/** @var $me \Carrooi\ImagesManager\Latte\Macros */

		$me->addMacro('image', array($me, 'macroImage'));
		$me->addMacro('src', null, null, array($me, 'macroSrc'));

		$me->addMacro('isImage', array($me, 'macroIsImage'), '}');
		$me->addMacro('isNotImage', array($me, 'macroIsImage'), '}');
	}


	/**
	 * @param \Latte\MacroNode $node
	 * @param \Latte\PhpWriter $writer
	 * @return string
	 */
	public function macroImage(MacroNode $node, PhpWriter $writer)
	{
		return $writer->write("echo \\Carrooi\\ImagesManager\\Latte\\Macros::getUrl(\$template->getImagesManager(), %node.args);");
	}


	/**
	 * @param \Latte\MacroNode $node
	 * @param \Latte\PhpWriter $writer
	 * @return string
	 */
	public function macroSrc(MacroNode $node, PhpWriter $writer)
	{
		return $writer->write("echo ' src=\"'. \\Carrooi\\ImagesManager\\Latte\\Macros::getUrl(\$template->getImagesManager(), %node.args). '\"';");
	}


	/**
	 * @param \Latte\MacroNode $node
	 * @param \Latte\PhpWriter $writer
	 * @return string
	 */
	public function macroIsImage(MacroNode $node, PhpWriter $writer)
	{
		$not = $node->name === 'isNotImage' ? '!' : '';
		$code = "if ($not\$template->getImagesManager()->createImage(%node.args)->isExists()) {";

		return $writer->write($code);
	}


	/**
	 * @param \Carrooi\ImagesManager\ImagesManager $imagesManager
	 * @return string
	 */
	public static function getUrl(ImagesManager $imagesManager)
	{
		$args = func_get_args();
		array_shift($args);

		try {
			$absolute = false;
			if ((is_string($args[1]) || is_int($args[1])) && strpos($args[1], '//') === 0) {
				$absolute = true;
				$args[1] = substr($args[1], 2);
			}

			/** @var \Carrooi\ImagesManager\Image $image */
			$image = call_user_func_array(array($imagesManager, 'load'), $args);

			return $image->getUrl($absolute);
		} catch (ImageNotExistsException $e) {
			if (count($args) > 2) {<commit_msg>Latte: fix isImage and isNotImage macros when image is missing<commit_after><?php

namespace Carrooi\ImagesManager\Latte;

use Carrooi\ImagesManager\Helpers as ImagesHelpers;
use Carrooi\ImagesManager\ImageNotExistsException;
use Carrooi\ImagesManager\ImagesManager;
use Latte\Macros\MacroSet;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\PhpWriter;

/**
 *
 * @author <NAME>
 */
class Macros extends MacroSet
{


	/**
	 * @param \Latte\Compiler $compiler
	 */
	public static function install(Compiler $compiler)
	{
		$me = new static($compiler);		/** @var $me \Carrooi\ImagesManager\Latte\Macros */

		$me->addMacro('image', array($me, 'macroImage'));
		$me->addMacro('src', null, null, array($me, 'macroSrc'));

		$me->addMacro('isImage', '$_imageCurrent = $template->getImagesManager()->createImage(%node.args); if ($_imageCurrent && $_imageCurrent->isExists()) {', '}');
		$me->addMacro('isNotImage', '$_imageCurrent = $template->getImagesManager()->createImage(%node.args); if (!$_imageCurrent || !$_imageCurrent->isExists()) {', '}');
	}


	/**
	 * @param \Latte\MacroNode $node
	 * @param \Latte\PhpWriter $writer
	 * @return string
	 */
	public function macroImage(MacroNode $node, PhpWriter $writer)
	{
		return $writer->write("echo \\Carrooi\\ImagesManager\\Latte\\Macros::getUrl(\$template->getImagesManager(), %node.args);");
	}


	/**
	 * @param \Latte\MacroNode $node
	 * @param \Latte\PhpWriter $writer
	 * @return string
	 */
	public function macroSrc(MacroNode $node, PhpWriter $writer)
	{
		return $writer->write("echo ' src=\"'. \\Carrooi\\ImagesManager\\Latte\\Macros::getUrl(\$template->getImagesManager(), %node.args). '\"';");
	}


	/**
	 * @param \Carrooi\ImagesManager\ImagesManager $imagesManager
	 * @return string
	 */
	public static function getUrl(ImagesManager $imagesManager)
	{
		$args = func_get_args();
		array_shift($args);

		try {
			$absolute = false;
			if ((is_string($args[1]) || is_int($args[1])) && strpos($args[1], '//') === 0) {
				$absolute = true;
				$args[1] = substr($args[1], 2);
			}

			/** @var \Carrooi\ImagesManager\Image $image */
			$image = call_user_func_array(array($imagesManager, 'load'), $args);

			return $image->getUrl($absolute);
		} catch (ImageNotExistsException $e) {
			if (count($args) > 2) {
{'fraction_non_alphanumeric': 0.18802498384665087, 'fraction_numerical': 0.0034460478139134183, 'mean_word_length': 4.418903150525088, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4970743', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1589, 'n_words': 299}