<commit_before><?php

/*
 * This file is part of Phraseanet SDK.
 *
 * (c) Alchemy <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PhraseanetSDK;

use PhraseanetSDK\Http\APIGuzzleAdapter;
use PhraseanetSDK\Repository\Factory as RepoFactory;
use PhraseanetSDK\Entity\Factory as EntityFactory;
use PhraseanetSDK\Entity\EntityHydrator;
use PhraseanetSDK\Entity\EntityInterface;

class EntityManager
{
    private $adapter;
    private $repositories;

    /**
     *
     * @param APIGuzzleAdapter $adapter
     */
    public function __construct(APIGuzzleAdapter $adapter)
    {
        $this->adapter = $adapter;
    }

    /**
     * Get a repository by its name
     *
     * @param  string                                        $name
     * @return \PhraseanetSDK\Repository\RepositoryInterface
     */
    public function getRepository($name)
    {
        if (isset($this->repositories[$name])) {
            return $this->repositories[$name];
        }

        $this->repositories[$name] = RepoFactory::build($name, $this);

        return $this->repositories[$name];
    }

    /**
     * Return a new entity by its name
     *
     * @param  string          $name The name of the entity
     * @return EntityInterface
     */
    public function getEntity($name)
    {
        return EntityFactory::build($name, $this);
    }

    /**
     * Hydrates an entity with datas
     *
     * @param EntityInterface $entity
     * @param \stdClass       $datas
     *
     * @return EntityInterface
     */
    public function HydrateEntity(EntityInterface $entity, \stdClass $datas)
    {
        return EntityHydrator::hydrate($entity, $datas, $this);
    }

    /**
     * Return the client attached to this entity manager
     *
     * @return APIGuzzleAdapter
     */
    public function getAdapter()
    {
        return $this->adapter;
    }
}<commit_msg>Update entity manager to handle annotations & proxies<commit_after><?php

/*
 * This file is part of Phraseanet SDK.
 *
 * (c) Alchemy <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PhraseanetSDK;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\FileCacheReader;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PhraseanetSDK\Http\APIGuzzleAdapter;
use PhraseanetSDK\Repository\Factory as RepoFactory;
use PhraseanetSDK\Entity\Factory as EntityFactory;
use PhraseanetSDK\Entity\EntityInterface;
use PhraseanetSDK\VirtualProxy\VirtualProxyFactory;
use ProxyManager\Configuration as ProxyConfig;
use ProxyManager\Factory\LazyLoadingGhostFactory;
use Psr\Log\LoggerInterface;

class EntityManager
{
    private $adapter;
    private $annotationReader;
    private $proxyFactory;
    private $repositories = array();
    private $virtualProxies = array();

    /**
     * @param APIGuzzleAdapter $adapter
     */
    public function __construct(APIGuzzleAdapter $adapter, array $options = array())
    {
        $this->adapter = $adapter;
        self::registerAnnotations();

        $debug = isset($options['debug']) && !!$options['debug'];
        $annotationsPath = isset($options['annotation.path']) ? $options['annotation.path'] : __DIR__.'/../../cache/annotations';
        $proxyPath = isset($options['proxy.path']) ? $options['proxy.path'] : __DIR__ . '/../../cache/proxies';

        $logger = (isset($options['logger']) && $options['logger'] instanceof LoggerInterface) ? $options['logger'] : null;

        if (!$logger && $debug) {
            $logger = new Logger('phraseanet-php-sdk');
            $logger->pushHandler(new StreamHandler(__DIR__ . '/../../log/doctrine.log'));
        }

        $this->annotationReader = new FileCacheReader(
            new AnnotationReader(),
            $annotationsPath,
            $debug
        );

        $config = new ProxyConfig();
        $config->setProxiesTargetDir($proxyPath);

        spl_autoload_register($config->getProxyAutoloader());

        $this->proxyFactory = new LazyLoadingGhostFactory($config);

        $this->logger = $logger;
    }

    /**
     * @return Logger|null
     */
    public function getLogger()
    {
        return $this->logger;
    }

    /**
     * @return FileCacheReader
     */
    public function getAnnotationReader()
    {
        return $this->annotationReader;
    }

    /**
     * @return LazyLoadingGhostFactory
     */
    public function getProxyFactory()
    {
        return $this->proxyFactory;
    }

    /**
     * Return the client attached to this entity manager
     *
     * @return APIGuzzleAdapter
     */
    public function getAdapter()
    {
        return $this->adapter;
    }

    /**
     * Get a repository by its name
     *
     * @param  string                                        $name
     * @return \PhraseanetSDK\Repository\RepositoryInterface
     */
    public function getRepository($name)
    {
        if (isset($this->repositories[$name])) {
            return $this->repositories[$name];
        }

        $className = ucfirst($name);
        $objectName = sprintf('\\PhraseanetSDK\\Repository\\%s', $className);

        if (!class_exists($objectName)) {
            throw new Exception\InvalidArgumentException(
                sprintf('Class %s does not exists', $objectName)
            );
        }

        return $this->repositories[$name] = new $objectName($this);
    }

    /**
     * Return virtual proxy for given entity
     *
     * @param $name
     *
     * @return mixed
     */
    public function getVirtualProxy($name)
    {
        if (isset($this->virtualProxies[$name])) {
            return $this->virtualProxies[$name];
        }

        $className = ucfirst($name);
        $objectName = sprintf('\\PhraseanetSDK\\VirtualProxy\\%s', $className);

        if (!class_exists($objectName)) {
            throw new Exception\InvalidArgumentException(
                sprintf('Class %s does not exists', $objectName)
            );
        }

        return $this->virtualProxies[$name] = new $objectName($this);
    }

    /**
     * Register entities annotations
     */
    private static function registerAnnotations()
    {
        AnnotationRegistry::registerFile(__DIR__.'/Annotation/ApiField.php');
        AnnotationRegistry::registerFile(__DIR__.'/Annotation/ApiObject.php');
        AnnotationRegistry::registerFile(__DIR__.'/Annotation/ApiRelation.php');
    }
}
{'fraction_non_alphanumeric': 0.12019957665557908, 'fraction_numerical': 0.0, 'mean_word_length': 2.5167464114832536, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5813408', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1816, 'n_words': 408}