/** * @param string $name * @param array $config For available options see Repository::$config * @param string $label * * @return Repository */ public function createRepository($name, array $config, $label = null) { $repository = new Repository($name, $config); $repository->setLabel($label); $repository->init($this->container); $this->em->persist($repository); $this->em->flush(); return $repository; }
public function testInterceptors() { $itc = new DummyInterceptor(); $interceptorsProvider = \Phake::mock('Modera\\FileRepositoryBundle\\Intercepting\\InterceptorsProviderInterface'); \Phake::when($interceptorsProvider)->getInterceptors($this->isInstanceOf(Repository::clazz()))->thenReturn([$itc]); $container = \Phake::mock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); \Phake::when($container)->get('modera_file_repository.intercepting.interceptors_provider')->thenReturn($interceptorsProvider); $repository = new Repository('foo', array('filesystem' => 'foo')); $repository->init($container); $splFile = new \SplFileInfo(__FILE__); $storedFile = \Phake::mock(StoredFile::clazz()); // --- $this->assertEquals(0, count($itc->beforePutInvocations)); $repository->beforePut($splFile); $this->assertEquals(1, count($itc->beforePutInvocations)); $this->assertSame($splFile, $itc->beforePutInvocations[0][0]); $this->assertSame($repository, $itc->beforePutInvocations[0][1]); // --- $receivedInterceptor = null; $repository->beforePut($splFile, function ($interceptor) use(&$receivedInterceptor) { $receivedInterceptor = $interceptor; return false; }); $this->assertSame($itc, $receivedInterceptor); $this->assertEquals(1, count($itc->beforePutInvocations)); // --- $this->assertEquals(0, count($itc->onPutInvocations)); $repository->onPut($storedFile, $splFile); $this->assertEquals(1, count($itc->onPutInvocations)); $this->assertSame($storedFile, $itc->onPutInvocations[0][0]); $this->assertSame($splFile, $itc->onPutInvocations[0][1]); $this->assertSame($repository, $itc->onPutInvocations[0][2]); // --- $receivedInterceptor = null; $repository->onPut($storedFile, $splFile, function ($interceptor) use(&$receivedInterceptor) { $receivedInterceptor = $interceptor; return false; }); $this->assertSame($itc, $receivedInterceptor); $this->assertEquals(1, count($itc->onPutInvocations)); // --- $this->assertEquals(0, count($itc->afterPutInvocations)); $repository->afterPut($storedFile, $splFile); $this->assertEquals(1, count($itc->afterPutInvocations)); $this->assertSame($storedFile, $itc->afterPutInvocations[0][0]); $this->assertSame($splFile, $itc->afterPutInvocations[0][1]); $this->assertSame($repository, $itc->afterPutInvocations[0][2]); $receivedInterceptor = null; $repository->afterPut($storedFile, $splFile, function ($interceptor) use(&$receivedInterceptor) { $receivedInterceptor = $interceptor; return false; }); $this->assertSame($itc, $receivedInterceptor); $this->assertEquals(1, count($itc->afterPutInvocations)); }
/** * {@inheritdoc} */ public function getInterceptors(Repository $repository) { $interceptors = array(); $ids = array('modera_file_repository.validation.file_properties_validation_interceptor'); $config = $repository->getConfig(); if (isset($config['interceptors']) && is_array($config['interceptors'])) { $ids = array_merge($ids, $config['interceptors']); } foreach ($ids as $id) { $interceptors[] = $this->container->get($id); } return $interceptors; }
public function testHowWellContainerIsInjected() { $repository = new Repository('test repo', array('filesystem' => '', 'storage_key_generator' => '')); self::$em->persist($repository); self::$em->flush(); self::$em->clear(); /* @var Repository $repository */ $repository = self::$em->getRepository(Repository::clazz())->find($repository->getId()); $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\ContainerInterface', Toolkit::getPropertyValue($repository, 'container')); }
/** * {@inheritdoc} */ public function beforePut(\SplFileInfo $file, Repository $repository) { $config = $repository->getConfig(); $wrapper = new FileWrapper($file); if (isset($config['images_only']) && true === $config['images_only']) { $wrapper->addImageConstraint(); } if (isset($config['max_size']) && '' != $config['max_size']) { $wrapper->addFileConstraint(array('maxSize' => $config['max_size'])); } if (isset($config['file_constraint']) && is_array($config['file_constraint'])) { $wrapper->addFileConstraint($config['file_constraint']); } if (isset($config['image_constraint']) && is_array($config['image_constraint'])) { $wrapper->addImageConstraint($config['image_constraint']); } $errors = $wrapper->validate($this->validator); if (count($errors)) { throw FileValidationException::create($file, $errors, $repository); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { /* @var EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $rows = array(); foreach ($em->getRepository(Repository::clazz())->findAll() as $repository) { /* @var Repository $repository */ $config = $repository->getConfig(); $rows[] = array($repository->getId(), $repository->getName(), $repository->getLabel() ? $repository->getLabel() : '-', $config['filesystem'], isset($config['overwrite_files']) ? $config['overwrite_files'] : false, $config['storage_key_generator']); } /* @var TableHelper $table */ $table = $this->getHelperSet()->get('table'); $table->setHeaders(array('#', 'Name', 'Label', 'Filesystem', 'Overwrite files', 'Storage key generator'))->setRows($rows); $table->render($output); }
public function testGetInterceptors() { $wannabeInterceptor = new \stdClass(); $anotherWannabeInterceptor = new \stdClass(); $container = \Phake::mock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); \Phake::when($container)->get('modera_file_repository.validation.file_properties_validation_interceptor')->thenReturn($wannabeInterceptor); \Phake::when($container)->get('foo_interceptor')->thenReturn($anotherWannabeInterceptor); $repository = \Phake::mock(Repository::clazz()); $provider = new DefaultInterceptorsProvider($container); $result = $provider->getInterceptors($repository); $this->assertEquals(1, count($result)); $this->assertSame($wannabeInterceptor, $result[0]); // and now with a "interceptors" config: \Phake::when($repository)->getConfig()->thenReturn(array('interceptors' => array('foo_interceptor'))); $result = $provider->getInterceptors($repository); $this->assertEquals(2, count($result)); $this->assertSame($wannabeInterceptor, $result[0]); $this->assertSame($anotherWannabeInterceptor, $result[1]); }
/** * {@inheritdoc} */ public static function doTearDownAfterClass() { self::$st->dropSchema(array(self::$em->getClassMetadata(Repository::clazz()), self::$em->getClassMetadata(StoredFile::clazz()))); }
public function testHowWellItWorks() { /* @var FileRepository $fr */ $fr = self::$container->get('modera_file_repository.repository.file_repository'); $this->assertNull($fr->getRepository('dummy_repository')); $repositoryConfig = array('storage_key_generator' => 'modera_file_repository.repository.uniqid_key_generator', 'filesystem' => 'dummy_tmp_fs'); $this->assertFalse($fr->repositoryExists('dummy_repository')); $repository = $fr->createRepository('dummy_repository', $repositoryConfig, 'My dummy repository'); $this->assertTrue($fr->repositoryExists('dummy_repository')); $this->assertInstanceOf(Repository::clazz(), $repository); $this->assertNotNull($repository->getId()); $this->assertEquals('dummy_repository', $repository->getName()); $this->assertEquals('My dummy repository', $repository->getLabel()); $this->assertSame($repositoryConfig, $repository->getConfig()); $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\ContainerInterface', Toolkit::getPropertyValue($repository, 'container')); // --- $fileContents = 'foo contents'; $filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'our-dummy-file.txt'; file_put_contents($filePath, $fileContents); $file = new File($filePath); $storedFile = $fr->put($repository->getName(), $file, array()); self::$em->clear(); // this way we will make sure that data is actually persisted in database /* @var StoredFile $storedFile */ $storedFile = self::$em->find(StoredFile::clazz(), $storedFile->getId()); $this->assertInstanceOf(StoredFile::clazz(), $storedFile); $this->assertNotNull($storedFile->getId()); $this->assertNotNull($storedFile->getStorageKey()); $this->assertEquals('our-dummy-file.txt', $storedFile->getFilename()); $this->assertNotNull($storedFile->getCreatedAt()); $this->assertEquals('txt', $storedFile->getExtension()); $this->assertEquals('text/plain', $storedFile->getMimeType()); $this->assertSame($repository->getId(), $storedFile->getRepository()->getId()); $this->assertEquals($fileContents, $storedFile->getContents()); $this->assertTrue('' != $storedFile->getChecksum()); $this->assertEquals($file->getSize(), $storedFile->getSize()); // --- $storedFileData = array('id' => $storedFile->getId(), 'storageKey' => $storedFile->getStorageKey(), 'filename' => $storedFile->getFilename()); $fileContents = 'bar contents'; $filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bar-dummy-file.txt'; file_put_contents($filePath, $fileContents); $file = new File($filePath); $storedFile = $fr->put($repository->getName(), $file, array()); self::$em->clear(); // this way we will make sure that data is actually persisted in database /* @var StoredFile $storedFile */ $storedFile = self::$em->find(StoredFile::clazz(), $storedFile->getId()); $this->assertNotEquals($storedFileData['id'], $storedFile->getId()); $this->assertNotEquals($storedFileData['storageKey'], $storedFile->getStorageKey()); $this->assertNotEquals($storedFileData['filename'], $storedFile->getFilename()); // --- $repositoryConfig['overwrite_files'] = true; $repository = $storedFile->getRepository(); $repository->setConfig($repositoryConfig); self::$em->persist($repository); self::$em->flush(); $storedFileData = array('id' => $storedFile->getId(), 'storageKey' => $storedFile->getStorageKey(), 'filename' => $storedFile->getFilename()); $storedFile = $fr->put($repository->getName(), $file, array()); self::$em->clear(); // this way we will make sure that data is actually persisted in database /* @var StoredFile $storedFile */ $storedFile = self::$em->find(StoredFile::clazz(), $storedFile->getId()); $this->assertEquals($storedFileData['id'], $storedFile->getId()); $this->assertEquals($storedFileData['storageKey'], $storedFile->getStorageKey()); $this->assertEquals($storedFileData['filename'], $storedFile->getFilename()); // --- $fs = $storedFile->getRepository()->getFilesystem(); $this->assertTrue($fs->has($storedFile->getStorageKey())); self::$em->remove($storedFile); self::$em->flush(); $this->assertFalse($fs->has($storedFile->getStorageKey())); }
/** * This method is going to be invoked recursively to generate thumbnails, classes with AlternativeFileTrait are * going to serve as markers that thumbnail needs to be generated. * * {@inheritdoc} */ public function onPut(StoredFile $storedFile, \SplFileInfo $file, Repository $repository) { $repoConfig = $storedFile->getRepository()->getConfig(); if (!isset($repoConfig['thumbnail_sizes']) || count($repoConfig['thumbnail_sizes']) == 0) { return self::RESULT_NO_CONFIG_AVAILABLE; } $isAlternative = $this->isAlternative($file); // given $storedFile and $file could be alternative already so we need to resolve // an original file that we are going to use to generate a thumbnail from if ($isAlternative) { /* @var AlternativeFileTrait $file */ $originalStoredFile = $file->getOriginalStoredFile(); $originalFile = $file->getOriginalFile(); $originalStoredFile->addAlternative($storedFile); } else { $originalStoredFile = $storedFile; $originalFile = $file; } if (!$originalFile instanceof File) { // "File" class provides the API we need (like guessing MIME type) $originalFile = new File($originalFile->getPathname()); } $isImage = substr($originalFile->getMimeType(), 0, strlen('image/')) == 'image/'; if (!$isImage) { return self::RESULT_NOT_IMAGE_GIVEN; } $lookupKey = $originalFile->getPathname(); if (!isset($this->thumbnailsProgress[$lookupKey])) { $this->thumbnailsProgress[$lookupKey] = $repoConfig['thumbnail_sizes']; // thumbnails that have yet to be generated } if ($isAlternative) { // Taking a config of previously generated thumbnail and updating entity to store it. // We couldn't update it right away when we generate a thumbnail because we haven't yet // had access to entity $this->thumbnailsGenerator->updateStoredFileAlternativeMeta($storedFile, $file->getThumbnailConfig()); } // getting next thumbnail's config that needs to be generated $thumbnailConfig = array_shift($this->thumbnailsProgress[$lookupKey]); if (!$thumbnailConfig) { // making it possible to have thumbnails generated if the same original file is passed to file // repository unset($this->thumbnailsProgress[$lookupKey]); return self::RESULT_NO_MORE_THUMBNAILS; } $thumbnailPathname = $this->generateThumbnail($originalFile, $storedFile, $thumbnailConfig); /* @var AlternativeFileTrait $newFile */ $newFile = null; if ($originalFile instanceof UploadedFile) { $newFile = new AlternativeUploadedFile($thumbnailPathname, $originalFile->getClientOriginalName(), $originalFile->getClientMimeType(), filesize($thumbnailPathname)); } else { $newFile = new AlternativeFile($thumbnailPathname); } $newFile->setOriginalFile($originalFile); $newFile->setOriginalStoredFile($originalStoredFile); $newFile->setThumbnailConfig($thumbnailConfig); // recursively creating thumbnails $this->fileRepository->put($repository->getName(), $newFile); // ... we cannot modify returned storedFile here already because UoW is already flushed return self::RESULT_SCHEDULED; }
/** * @return string */ public function getChecksum() { return $this->repository->getFilesystem()->checksum($this->storageKey); }