<commit_before><?php

namespace Symfony\Cmf\Bundle\BlockBundle\Block;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Sonata\BlockBundle\Block\BlockLoaderInterface;
use Sonata\BlockBundle\Model\BlockInterface;

/**
 * The PHPCR block loader loads from phpcr-odm, both by absolute path and
 * by path relative to the contentDocument in the request attributes.
 */
class PHPCRBlockLoader implements BlockLoaderInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var null|LoggerInterface
     */
    protected $logger;


    /**
     * @param ContainerInterface $container
     * @param string $documentManagerName
     */
    public function __construct(ContainerInterface $container, $documentManagerName, LoggerInterface $logger = null)
    {
        $this->container = $container;
        $this->dm = $this->container->get('doctrine_phpcr')->getManager($documentManagerName);
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function load($configuration)
    {
        if ($this->support($configuration)) {
            $block = $this->findByName($configuration['name']);
            if (null === $block) {
                // not found or no valid block
                return null;
            }

            // merge settings
            $userSettings = isset($configuration['settings']) && is_array($configuration['settings']) ?
                $configuration['settings'] :
                array()
            ;
            $defaultSettings = is_array($block->getSettings()) ? $block->getSettings() : array();
            $block->setSettings(array_merge($userSettings, $defaultSettings));

            return $block;
        }

        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function support($configuration)
    {
        if (!is_array($configuration)) {
            return false;
        }

        if (!isset($configuration['name'])) {
            return false;
        }

        return true;
    }

    /**
     * Finds one block by the given name
     *
     * @param string $name
     *
     * @return BlockInterface|null the block or null if not found/no BlockInterface at that location
     */
    protected function findByName($name)
    {
        if ($this->isAbsolutePath($name)) {
            $block = $this->dm->find(null, $name);
        } else if ($this->container->has('request')
            && $this->container->get('request')->attributes->has('contentDocument')
        ) {
            $currentPage = $this->container->get('request')->attributes->get('contentDocument');
            $block = $this->dm->find(null,  $currentPage->getPath() . '/' . $name);
        } else {
            if ($this->logger) {
                $this->logger->debug("Block '$name' is not absolute path and either there is no contentDocument or the relative path does not match");
            }

            return null;<commit_msg>tweaked error handling when attempting to load a block<commit_after><?php

namespace Symfony\Cmf\Bundle\BlockBundle\Block;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Sonata\BlockBundle\Block\BlockLoaderInterface;
use Sonata\BlockBundle\Model\BlockInterface;

/**
 * The PHPCR block loader loads from phpcr-odm, both by absolute path and
 * by path relative to the contentDocument in the request attributes.
 */
class PHPCRBlockLoader implements BlockLoaderInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var null|LoggerInterface
     */
    protected $logger;


    /**
     * @param ContainerInterface $container
     * @param string $documentManagerName
     */
    public function __construct(ContainerInterface $container, $documentManagerName, LoggerInterface $logger = null)
    {
        $this->container = $container;
        $this->dm = $this->container->get('doctrine_phpcr')->getManager($documentManagerName);
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function load($configuration)
    {
        if ($this->support($configuration)) {
            $block = $this->findByName($configuration['name']);
            if (null === $block) {
                // not found or no valid block
                return null;
            }

            // merge settings
            $userSettings = isset($configuration['settings']) && is_array($configuration['settings']) ?
                $configuration['settings'] :
                array()
            ;
            $defaultSettings = is_array($block->getSettings()) ? $block->getSettings() : array();
            $block->setSettings(array_merge($userSettings, $defaultSettings));

            return $block;
        }

        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function support($configuration)
    {
        if (!is_array($configuration)) {
            return false;
        }

        if (!isset($configuration['name'])) {
            return false;
        }

        return true;
    }

    /**
     * Finds one block by the given name
     *
     * @param string $name
     *
     * @return BlockInterface|null the block or null if not found/no BlockInterface at that location
     */
    protected function findByName($name)
    {
        if ($this->isAbsolutePath($name)) {
            $path = $name;
            $block = $this->dm->find(null, $name);
        } else if ($this->container->has('request')
            && $this->container->get('request')->attributes->has('contentDocument')
        ) {
            $currentPage = $this->container->get('request')->attributes->get('contentDocument');
            $path = $currentPage->getPath() . '/' . $name;
            $block = $this->dm->find(null, $path);
        }

        if (empty($block)) {
            if ($this->logger) {
                $msg = isset($path)
                    ? "Block '$name' at path '$path' could not be found."
                    : "Block '$name' is not absolute path and there is no request attribute with 'contentDocument'."
                ;
                $this->logger->debug($msg);
            }

            return null;
{'fraction_non_alphanumeric': 0.13062132528205944, 'fraction_numerical': 0.0, 'mean_word_length': 2.190065889508363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '5493507', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1600, 'n_words': 379}