├── .editorconfig ├── .formatter.yml ├── .gitignore ├── .php_cs ├── .travis.yml ├── CHANGELOG-0.1.md ├── CompilerPass └── Abstracts │ └── AbstractMappingCompilerPass.php ├── Configurator └── LocatorConfigurator.php ├── DataObject └── EntityMapping.php ├── Exception ├── ConfigurationInvalidException.php ├── EntityManagerNotFoundException.php └── MappingExtensionWithoutDriverException.php ├── LICENSE ├── Locator └── SimpleDoctrineMappingLocator.php ├── README.md ├── Tests ├── Miscelania │ ├── MappingAliasTest.php │ ├── MappingCoexistingWithAutomappingTest.php │ └── MappingTest.php └── TestBundle │ ├── CompilerPass │ ├── ExtendedMappingCompilerPass.php │ ├── MappingCompilerPass.php │ └── SimpleMappingCompilerPass.php │ ├── DependencyInjection │ └── TestExtension.php │ ├── Entity │ ├── Class1.php │ └── Class2.php │ ├── ExtendedTestBundle.php │ ├── Mapping │ ├── Class1.orm.yml │ ├── Class2.custom.orm.yml │ ├── Class3.orm.yaml │ └── Class4.orm.xml │ ├── MyOtherEntityNamespace │ ├── Class3.php │ └── Class4.php │ ├── Resources │ └── config │ │ └── doctrine │ │ ├── Class1.orm.yml │ │ └── Class2.orm.yml │ ├── SimpleTestBundle.php │ └── TestBundle.php ├── composer.json └── phpunit.xml.dist /.editorconfig: -------------------------------------------------------------------------------- 1 | ; top-most EditorConfig file 2 | root = true 3 | 4 | ; Unix-style newlines 5 | [*] 6 | end_of_line = LF 7 | 8 | [*.php] 9 | indent_style = space 10 | indent_size = 4 11 | -------------------------------------------------------------------------------- /.formatter.yml: -------------------------------------------------------------------------------- 1 | use-sort: 2 | group: 3 | - _main 4 | - Mmoreram 5 | group-type: each 6 | sort-type: alph 7 | sort-direction: asc 8 | strict: true 9 | header: | 10 | /* 11 | * SimpleDoctrineMapping for Symfony2 12 | * 13 | * For the full copyright and license information, please view the LICENSE 14 | * file that was distributed with this source code. 15 | * 16 | * Feel free to edit as you please, and have fun. 17 | * 18 | * @author Marc Morera 19 | */ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.phar 3 | composer.lock 4 | phpunit.xml 5 | /bin -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | level(Symfony\CS\FixerInterface::SYMFONY_LEVEL) 6 | // and extra fixers: 7 | ->fixers([ 8 | 'concat_with_spaces', 9 | 'multiline_spaces_before_semicolon', 10 | 'short_array_syntax', 11 | '-remove_lines_between_uses', 12 | '-empty_return', 13 | '-phpdoc_var_without_name', 14 | '-phpdoc_to_comment', 15 | ]); 16 | 17 | if (null === $input->getArgument('path')) { 18 | $config 19 | ->finder( 20 | Symfony\CS\Finder\DefaultFinder::create() 21 | ->in('.') 22 | ->exclude('vendor') 23 | ); 24 | } 25 | 26 | return $config; 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | sudo: false 4 | 5 | matrix: 6 | include: 7 | - php: 7.1 8 | env: $SYMFONY_VERSION="^3.2" 9 | 10 | before_install: 11 | - 'if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/symfony:${SYMFONY_VERSION}; fi;' 12 | 13 | install: 14 | - composer update --prefer-source --no-interaction ${COMPOSER_OPTIONS} 15 | 16 | script: 17 | - php vendor/bin/phpunit 18 | 19 | notifications: 20 | email: false 21 | -------------------------------------------------------------------------------- /CHANGELOG-0.1.md: -------------------------------------------------------------------------------- 1 | CHANGELOG for 0.1.x 2 | =================== 3 | 4 | This changelog references the relevant changes done in 0.1.x versions 5 | 6 | To get the diff for a specific change, go to 7 | https://github.com/mmoreram/SimpleDoctrineMapping/commit/XXX where XXX is the 8 | change hash To get the diff between two versions, go to 9 | https://github.com/mmoreram/SimpleDoctrineMapping/compare/v0.1.4...v0.1.5 10 | 11 | ### v0.1.6 (04-04-2015) 12 | 13 | * [`407d4e3`](https://github.com/elcodi/elcodi/commit/407d4e3b16e3046aa893d9e6b136107b91401a89) Some small improves (mmoreram) 14 | * [`b2eb430`](https://github.com/elcodi/elcodi/commit/b2eb4301e3d3c3c298d3d20b1ef2a7d969dca9ac) fix docblock for mapping compiler pass methods (Luis Cordova) 15 | * [`fe723e8`](https://github.com/elcodi/elcodi/commit/fe723e813b2ac573f152bc6c3612babaf71d348c) Make compatible with doctrine extension (sfblaauw) 16 | * [`8d6d18a`](https://github.com/elcodi/elcodi/commit/8d6d18a94328245ac42934b11666240edfc8ffce) Added Exception when Class or Mapping wrong (mmoreram) 17 | * [`b747f74`](https://github.com/elcodi/elcodi/commit/b747f742e8ec4548b952867fbb42ee6aac3fdce5) [doc] Small typo (Roger Gros) 18 | 19 | ### v0.1.5 (27-12-2014) 20 | 21 | * [`fab4e74`](https://github.com/elcodi/elcodi/commit/fab4e743a156a1147d24253467690a2340472d05) Add the posibility to add driver namespace (sfblaauw) 22 | 23 | ### v0.1.4 (15-12-2014) 24 | 25 | * [`3940f4c`](https://github.com/elcodi/elcodi/commit/3940f4c14648379934045bfbc366036c36f972b6) fix absolute paths in LocatorConfigurator (Berny Cantos) 26 | * [`e15e41b`](https://github.com/elcodi/elcodi/commit/e15e41b7455e8a55a8b81e463a10fbf466eb9092) Update README.md (ysramirez) 27 | 28 | ### v0.1.3 (22-10-2014) 29 | 30 | * [`1986180`](https://github.com/elcodi/elcodi/commit/1986180fddf7a86c5eb5a2b72e4b1c658fe726fa) Issue #9 (mmoreram) 31 | 32 | ### v0.1.2 (03-09-2014) 33 | 34 | * [`d936789`](https://github.com/elcodi/elcodi/commit/d936789817e4e8eb992bf57ca26fe041483e91f0) Update .travis.yml (Luis Cordova) 35 | * [`459ff3a`](https://github.com/elcodi/elcodi/commit/459ff3a24294234243653a47af16174726042711) by default is stable (Luis Cordova) 36 | * [`dd316aa`](https://github.com/elcodi/elcodi/commit/dd316aaf94f9da255d3de85fc21bba3ab977c963) Update composer.json (Luis Cordova) 37 | * [`acf86ea`](https://github.com/elcodi/elcodi/commit/acf86eabd49243f15ee846f6eca314cdd514406c) updates to the README.md (Luis Cordova) 38 | 39 | ### v0.1.1 (01-09-2014) 40 | 41 | * [`a257456`](https://github.com/elcodi/elcodi/commit/a25745639ab484166fd01dc8fbdc2d529deee751) Updated formatter with new header info (mmoreram) 42 | * [`4a6b0fe`](https://github.com/elcodi/elcodi/commit/4a6b0feda32da40919a8f6edfc2998ec0867848b) Fixed DIRECTORY_SEPARATOR usage for win platforms (mmoreram) 43 | -------------------------------------------------------------------------------- /CompilerPass/Abstracts/AbstractMappingCompilerPass.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\CompilerPass\Abstracts; 17 | 18 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 19 | use Symfony\Component\DependencyInjection\ContainerBuilder; 20 | use Symfony\Component\DependencyInjection\Definition; 21 | use Symfony\Component\DependencyInjection\Reference; 22 | 23 | use Mmoreram\SimpleDoctrineMapping\DataObject\EntityMapping; 24 | use Mmoreram\SimpleDoctrineMapping\Exception\ConfigurationInvalidException; 25 | use Mmoreram\SimpleDoctrineMapping\Exception\EntityManagerNotFoundException; 26 | use Mmoreram\SimpleDoctrineMapping\Exception\MappingExtensionWithoutDriverException; 27 | 28 | /** 29 | * Class MappingCompilerPass. 30 | */ 31 | abstract class AbstractMappingCompilerPass implements CompilerPassInterface 32 | { 33 | /** 34 | * Add mapping entity. 35 | * 36 | * This method adds a new Driver into global MappingDriverChain with single 37 | * entity mapping information. 38 | * 39 | * $entityManagerName must be an existing entityManager. By default doctrine 40 | * creates just one common EntityManager called default, but many can be 41 | * defined with different connection information 42 | * 43 | * p.e. default 44 | * p.e. anotherEntityManager 45 | * 46 | * $entityNamespace must be an existing namespace of Entity. This value also 47 | * can be a valid and existing container parameter, with an existing 48 | * namespace of Entity as value. 49 | * 50 | * p.e. MyBundle\Entity\User 51 | * p.e. mybundle.entity.user.class 52 | * 53 | * $entityMappingFilePath must be a path of an existing yml or xml file with 54 | * mapping information about $entityNamespace. This bundle uses Short Bundle 55 | * notation, with "@" symbol. This value also can be a valid and existing 56 | * container parameter, with a path of an existing yml or xml file as value. 57 | * 58 | * p.e. @MyBundle/Resources/config/doctrine/User.orm.yml 59 | * p.e. @MyBundle/Resources/config/doctrine/User.orm.xml 60 | * p.e. mybundle.entity.user.mapping_file_path 61 | * 62 | * Finally, $enable flag just allow you to add current mapping definition 63 | * into all Doctrine Map table, or just dismiss it. This is useful when you 64 | * want to give possibility to final user to enable or disable a mapping 65 | * class. 66 | * 67 | * @param ContainerBuilder $container Container 68 | * @param string $entityManagerName EntityManager name 69 | * @param string $entityNamespace Entity namespace 70 | * @param string $entityMappingFilePath Entity Mapping file path 71 | * @param bool|string $enable Entity mapping must be included 72 | * 73 | * @return AbstractMappingCompilerPass 74 | * 75 | * @throws EntityManagerNotFoundException Entity Manager nod found 76 | */ 77 | protected function addEntityMapping( 78 | ContainerBuilder $container, 79 | $entityManagerName, 80 | $entityNamespace, 81 | $entityMappingFilePath, 82 | $enable = true 83 | ) : self { 84 | $entityMapping = $this->resolveEntityMapping( 85 | $container, 86 | $entityManagerName, 87 | $entityNamespace, 88 | $entityMappingFilePath, 89 | $enable 90 | ); 91 | 92 | if ($entityMapping instanceof EntityMapping) { 93 | $this->registerLocatorConfigurator($container); 94 | $this->registerLocator($container, $entityMapping); 95 | $this->registerDriver($container, $entityMapping); 96 | $this->addDriverInDriverChain($container, $entityMapping); 97 | $this->addAliases($container, $entityMapping); 98 | } 99 | 100 | return $this; 101 | } 102 | 103 | /** 104 | * Resolve EntityMapping inputs and build a consistent EntityMapping object. 105 | * 106 | * This method returns null if the current entity has not been added 107 | * 108 | * @param ContainerBuilder $container 109 | * @param string $entityManagerName 110 | * @param string $entityNamespace 111 | * @param string $entityMappingFilePath 112 | * @param string|bool $enable 113 | * 114 | * @return EntityMapping|null 115 | * 116 | * @throws ConfigurationInvalidException Configuration invalid 117 | * @throws EntityManagerNotFoundException Entity Manager not found 118 | */ 119 | private function resolveEntityMapping( 120 | ContainerBuilder $container, 121 | string $entityManagerName, 122 | string $entityNamespace, 123 | string $entityMappingFilePath, 124 | $enable = true 125 | ) : ? EntityMapping { 126 | $enableEntityMapping = $this->resolveParameterName( 127 | $container, 128 | $enable 129 | ); 130 | 131 | if (false === $enableEntityMapping) { 132 | return null; 133 | } 134 | 135 | $entityNamespace = $this->resolveParameterName($container, $entityNamespace); 136 | 137 | if (!class_exists($entityNamespace)) { 138 | throw new ConfigurationInvalidException('Entity ' . $entityNamespace . ' not found'); 139 | } 140 | 141 | $entityMappingFilePath = $this->resolveParameterName($container, $entityMappingFilePath); 142 | $entityManagerName = $this->resolveParameterName($container, $entityManagerName); 143 | $this->resolveEntityManagerName($container, $entityManagerName); 144 | 145 | return new EntityMapping( 146 | $entityNamespace, 147 | $entityMappingFilePath, 148 | $entityManagerName 149 | ); 150 | } 151 | 152 | /** 153 | * Register locator configurator. 154 | * 155 | * @param ContainerBuilder $container 156 | */ 157 | private function registerLocatorConfigurator(ContainerBuilder $container) 158 | { 159 | $locatorConfiguratorId = 'simple_doctrine_mapping.locator_configurator'; 160 | if ($container->hasDefinition($locatorConfiguratorId)) { 161 | return; 162 | } 163 | 164 | $locatorConfigurator = new Definition('Mmoreram\SimpleDoctrineMapping\Configurator\LocatorConfigurator'); 165 | $locatorConfigurator->setPublic(true); 166 | $locatorConfigurator->setArguments([ 167 | new Reference('kernel'), 168 | ]); 169 | 170 | $container->setDefinition($locatorConfiguratorId, $locatorConfigurator); 171 | } 172 | 173 | /** 174 | * Register the locator. 175 | * 176 | * @param ContainerBuilder $container 177 | * @param EntityMapping $entityMapping 178 | */ 179 | private function registerLocator( 180 | ContainerBuilder $container, 181 | EntityMapping $entityMapping 182 | ) { 183 | /** 184 | * Locator. 185 | */ 186 | $locatorId = 'simple_doctrine_mapping.locator.' . $entityMapping->getUniqueIdentifier(); 187 | $locator = new Definition('Mmoreram\SimpleDoctrineMapping\Locator\SimpleDoctrineMappingLocator'); 188 | $locator->setPublic(false); 189 | $locator->setArguments([ 190 | $entityMapping->getEntityNamespace(), 191 | [$entityMapping->getEntityMappingFilePath()], 192 | ]); 193 | $locator->setConfigurator([ 194 | new Reference('simple_doctrine_mapping.locator_configurator'), 195 | 'configure', 196 | ]); 197 | $container->setDefinition($locatorId, $locator); 198 | } 199 | 200 | /** 201 | * Register the driver. 202 | * 203 | * @param ContainerBuilder $container 204 | * @param EntityMapping $entityMapping 205 | */ 206 | private function registerDriver( 207 | ContainerBuilder $container, 208 | EntityMapping $entityMapping 209 | ) { 210 | /** 211 | * Specific extension Driver definition. 212 | */ 213 | $mappingDriverId = 'doctrine.orm.' . $entityMapping->getUniqueIdentifier() . '_metadata_driver'; 214 | $mappingDriverDefinition = new Definition($this->getDriverNamespaceGivenEntityMapping($entityMapping)); 215 | $mappingDriverDefinition->setPublic(false); 216 | $mappingDriverDefinition->setArguments([ 217 | new Reference('simple_doctrine_mapping.locator.' . $entityMapping->getUniqueIdentifier()), 218 | ]); 219 | $mappingDriverDefinition->addMethodCall('setGlobalBasename', [ 220 | $entityMapping->getEntityNamespace(), 221 | ]); 222 | $container->setDefinition($mappingDriverId, $mappingDriverDefinition); 223 | } 224 | 225 | /** 226 | * Register and override the DriverChain definition. 227 | * 228 | * @param ContainerBuilder $container 229 | * @param EntityMapping $entityMapping 230 | * 231 | * @throws EntityManagerNotFoundException Entity Manager nod found 232 | */ 233 | private function addDriverInDriverChain( 234 | ContainerBuilder $container, 235 | EntityMapping $entityMapping 236 | ) { 237 | $chainDriverDefinition = $container 238 | ->getDefinition( 239 | 'doctrine.orm.' . $entityMapping->getEntityManagerName() . '_metadata_driver' 240 | ); 241 | 242 | $container->setParameter( 243 | 'doctrine.orm.metadata.driver_chain.class', 244 | 'Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain' 245 | ); 246 | 247 | $chainDriverDefinition->addMethodCall('addDriver', [ 248 | new Reference( 249 | 'doctrine.orm.' . $entityMapping->getUniqueIdentifier() . '_metadata_driver' 250 | ), 251 | $entityMapping->getEntityNamespace(), 252 | ]); 253 | } 254 | 255 | /** 256 | * Add aliases for short Doctrine accessing mode. 257 | * 258 | * This method will make a vitual association between the bundle proposing 259 | * the entity and the entity namespace, even if both elements are in 260 | * different packages (Bundle + Component). 261 | * 262 | * Only useful and working when the relation between a bundle and the entity 263 | * folder path [[ Bundle (*) => (1) Entity path ]] 264 | * 265 | * @param ContainerBuilder $container 266 | * @param EntityMapping $entityMapping 267 | */ 268 | private function addAliases( 269 | ContainerBuilder $container, 270 | EntityMapping $entityMapping 271 | ) { 272 | $entityMappingFilePath = $entityMapping->getEntityMappingFilePath(); 273 | if (strpos($entityMappingFilePath, '@') === 0) { 274 | $bundleName = trim(explode('/', $entityMappingFilePath, 2)[0]); 275 | $className = explode('\\', $entityMapping->getEntityNamespace()); 276 | unset($className[count($className) - 1]); 277 | $configurationServiceDefinition = $container 278 | ->getDefinition( 279 | 'doctrine.orm.' . $entityMapping->getEntityManagerName() . '_configuration' 280 | ); 281 | 282 | $configurationServiceDefinition->addMethodCall('addEntityNamespace', [ 283 | $bundleName, 284 | implode('\\', $className), 285 | ]); 286 | } 287 | } 288 | 289 | /** 290 | * Resolvers. 291 | */ 292 | 293 | /** 294 | * Return value of parameter name if exists 295 | * Return itself otherwise. 296 | * 297 | * @param ContainerBuilder $container 298 | * @param mixed $parameterName 299 | * 300 | * @return mixed 301 | */ 302 | private function resolveParameterName( 303 | ContainerBuilder $container, 304 | $parameterName 305 | ) { 306 | if (!is_string($parameterName)) { 307 | return $parameterName; 308 | } 309 | 310 | return $container->hasParameter($parameterName) 311 | ? $container->getParameter($parameterName) 312 | : $parameterName; 313 | } 314 | 315 | /** 316 | * Throws an exception if given entityName is not available or does 317 | * not exist. 318 | * 319 | * @param ContainerBuilder $container 320 | * @param string $entityManagerName 321 | * 322 | * @throws EntityManagerNotFoundException Entity manager not found 323 | */ 324 | private function resolveEntityManagerName( 325 | ContainerBuilder $container, 326 | string $entityManagerName 327 | ) { 328 | if (!$container->has('doctrine.orm.' . $entityManagerName . '_metadata_driver')) { 329 | throw new EntityManagerNotFoundException($entityManagerName); 330 | } 331 | } 332 | 333 | /** 334 | * Return the namespace of the driver to use given an EntityMapping. 335 | * 336 | * @param EntityMapping $entityMapping 337 | * 338 | * @return string 339 | * 340 | * @throws MappingExtensionWithoutDriverException Driver not found 341 | */ 342 | private function getDriverNamespaceGivenEntityMapping(EntityMapping $entityMapping) : string 343 | { 344 | $namespace = 'Doctrine\ORM\Mapping\Driver\\'; 345 | 346 | switch ($entityMapping->getExtension()) { 347 | case 'yml': 348 | $namespace .= 'YamlDriver'; 349 | break; 350 | case 'yaml': 351 | $namespace .= 'YamlDriver'; 352 | break; 353 | case 'xml': 354 | $namespace .= 'XmlDriver'; 355 | break; 356 | default: 357 | throw new MappingExtensionWithoutDriverException($entityMapping->getExtension()); 358 | } 359 | 360 | return $namespace; 361 | } 362 | } 363 | -------------------------------------------------------------------------------- /Configurator/LocatorConfigurator.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Configurator; 17 | 18 | use Symfony\Component\HttpKernel\Bundle\BundleInterface; 19 | use Symfony\Component\HttpKernel\KernelInterface; 20 | 21 | use Mmoreram\SimpleDoctrineMapping\Exception\ConfigurationInvalidException; 22 | use Mmoreram\SimpleDoctrineMapping\Locator\SimpleDoctrineMappingLocator; 23 | 24 | /** 25 | * Class LocatorConfigurator. 26 | */ 27 | final class LocatorConfigurator 28 | { 29 | /** 30 | * @var KernelInterface 31 | * 32 | * Kernel 33 | */ 34 | private $kernel; 35 | 36 | /** 37 | * Construct method. 38 | * 39 | * @param KernelInterface $kernel Kernel 40 | */ 41 | public function __construct(KernelInterface $kernel) 42 | { 43 | $this->kernel = $kernel; 44 | } 45 | 46 | /** 47 | * If there is a parameter named like the driver locator path, replace it 48 | * with the value of such parameter. 49 | * 50 | * @param SimpleDoctrineMappingLocator $locator Locator 51 | * 52 | * @throws ConfigurationInvalidException Configuration invalid 53 | */ 54 | public function configure(SimpleDoctrineMappingLocator $locator) 55 | { 56 | $path = $locator->getPaths()[0]; 57 | 58 | $resourcePathExploded = explode('/', $path); 59 | $resourcePathRoot = array_shift($resourcePathExploded); 60 | 61 | if (strpos($resourcePathRoot, '@') === 0) { 62 | $mappingFileBundle = ltrim($resourcePathRoot, '@'); 63 | $bundle = $this->kernel->getBundle($mappingFileBundle); 64 | 65 | if ($bundle instanceof BundleInterface) { 66 | $resourcePathRoot = $bundle->getPath(); 67 | } 68 | } 69 | 70 | array_unshift($resourcePathExploded, $resourcePathRoot); 71 | 72 | $path = implode('/', $resourcePathExploded); 73 | 74 | if (!file_exists($path)) { 75 | throw new ConfigurationInvalidException('Mapping file "' . $path . '" does not exist'); 76 | } 77 | 78 | $locator->setPaths([$path]); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /DataObject/EntityMapping.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\DataObject; 17 | 18 | /** 19 | * Class EntityMapping. 20 | */ 21 | final class EntityMapping 22 | { 23 | /** 24 | * @var string 25 | * 26 | * entityNamespace 27 | */ 28 | private $entityNamespace; 29 | 30 | /** 31 | * @var string 32 | * 33 | * entityMappingFilePath 34 | */ 35 | private $entityMappingFilePath; 36 | 37 | /** 38 | * @var string 39 | * 40 | * entityManagerName 41 | */ 42 | private $entityManagerName; 43 | 44 | /** 45 | * @var string 46 | * 47 | * UniqueIdentifier 48 | */ 49 | private $uniqueIdentifier; 50 | 51 | /** 52 | * @var string 53 | * 54 | * Extension 55 | */ 56 | private $extension; 57 | 58 | /** 59 | * Constructor. 60 | * 61 | * @param string $entityNamespace 62 | * @param string $entityMappingFilePath 63 | * @param string $entityManagerName 64 | */ 65 | public function __construct( 66 | string $entityNamespace, 67 | string $entityMappingFilePath, 68 | string $entityManagerName 69 | ) { 70 | $this->entityNamespace = $entityNamespace; 71 | $this->entityMappingFilePath = $entityMappingFilePath; 72 | $this->entityManagerName = $entityManagerName; 73 | $this->uniqueIdentifier = strtolower(str_replace('\\', '_', $entityNamespace)); 74 | $this->extension = pathinfo($entityMappingFilePath, PATHINFO_EXTENSION); 75 | } 76 | 77 | /** 78 | * Get EntityManagerName. 79 | * 80 | * @return string 81 | */ 82 | public function getEntityManagerName() : string 83 | { 84 | return $this->entityManagerName; 85 | } 86 | 87 | /** 88 | * Get EntityMappingFilePath. 89 | * 90 | * @return string 91 | */ 92 | public function getEntityMappingFilePath() : string 93 | { 94 | return $this->entityMappingFilePath; 95 | } 96 | 97 | /** 98 | * Get EntityNamespace. 99 | * 100 | * @return string 101 | */ 102 | public function getEntityNamespace() : string 103 | { 104 | return $this->entityNamespace; 105 | } 106 | 107 | /** 108 | * Get UniqueIdentifier. 109 | * 110 | * @return string 111 | */ 112 | public function getUniqueIdentifier() : string 113 | { 114 | return $this->uniqueIdentifier; 115 | } 116 | 117 | /** 118 | * Get Extension. 119 | * 120 | * @return string 121 | */ 122 | public function getExtension() : string 123 | { 124 | return $this->extension; 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Exception/ConfigurationInvalidException.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Exception; 17 | 18 | use Exception; 19 | 20 | /** 21 | * Class ConfigurationInvalidException. 22 | */ 23 | final class ConfigurationInvalidException extends Exception 24 | { 25 | } 26 | -------------------------------------------------------------------------------- /Exception/EntityManagerNotFoundException.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Exception; 17 | 18 | use Exception; 19 | 20 | /** 21 | * Class EntityManagerNotFoundException. 22 | */ 23 | final class EntityManagerNotFoundException extends Exception 24 | { 25 | /** 26 | * Exception constructor. 27 | * 28 | * @param string $entityManagerName 29 | */ 30 | public function __construct($entityManagerName) 31 | { 32 | parent::__construct('Entity manager "' . $entityManagerName . '" not found.'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Exception/MappingExtensionWithoutDriverException.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Exception; 17 | 18 | use Exception; 19 | 20 | /** 21 | * Class MappingExtensionWithoutDriverException. 22 | */ 23 | final class MappingExtensionWithoutDriverException extends Exception 24 | { 25 | /** 26 | * Exception constructor. 27 | * 28 | * @param string $extension 29 | */ 30 | public function __construct($extension) 31 | { 32 | parent::__construct('Mapping extension "' . $extension . '" is not managed by any driver. Please, consider using xml or yml extensions.'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Marc Morera 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is furnished 10 | to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /Locator/SimpleDoctrineMappingLocator.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Locator; 17 | 18 | use Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator; 19 | use Doctrine\ORM\Mapping\MappingException; 20 | 21 | /** 22 | * Class SimpleDoctrineMappingLocator. 23 | */ 24 | final class SimpleDoctrineMappingLocator extends DefaultFileLocator 25 | { 26 | /** 27 | * Entity namespace. 28 | * 29 | * @var string 30 | */ 31 | private $namespace; 32 | 33 | /** 34 | * Array with mapping file path. 35 | * 36 | * @var array 37 | */ 38 | private static $pathsMap = []; 39 | 40 | /** 41 | * Constructor. 42 | * 43 | * @param string $namespace 44 | * @param array $prefixes 45 | */ 46 | public function __construct($namespace, array $prefixes) 47 | { 48 | $this->namespace = $namespace; 49 | $this->paths = $prefixes; 50 | } 51 | 52 | /** 53 | * Set paths. 54 | * 55 | * @param array $paths 56 | */ 57 | public function setPaths($paths) 58 | { 59 | $this->paths = $paths; 60 | self::$pathsMap[$this->namespace] = $this->paths; 61 | } 62 | 63 | /** 64 | * {@inheritdoc} 65 | */ 66 | public function fileExists($className) 67 | { 68 | return isset($this->paths[0]) && is_file($this->paths[0]); 69 | } 70 | 71 | /** 72 | * {@inheritdoc} 73 | */ 74 | public function getAllClassNames($globalBasename = null) 75 | { 76 | return $globalBasename; 77 | } 78 | 79 | /** 80 | * {@inheritdoc} 81 | */ 82 | public function findMappingFile($className) 83 | { 84 | if (!$this->fileExists($className)) { 85 | throw MappingException::mappingFileNotFound($className, $this->paths[0]); 86 | } 87 | 88 | if (isset(self::$pathsMap[$className])) { 89 | $this->paths = self::$pathsMap[$className]; 90 | } 91 | 92 | return $this->paths[0]; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Doctrine Mapping for Symfony2 2 | 3 | [![Build Status](https://img.shields.io/travis/mmoreram/SimpleDoctrineMapping.svg?style=flat-square)](https://travis-ci.org/mmoreram/SimpleDoctrineMapping) 4 | 5 | *KISS*, Remember? 6 | 7 | This project provides you an amazing way of adding your entities mapping data 8 | in any Symfony project, without the need of taking part of the `auto_mapping` 9 | doctrine process. 10 | 11 | With a simple compiler pass in your bundle, make sure your entities are mapped 12 | properly into database, and provider your bundle users a unique way of 13 | overwriting and customization of these entities. 14 | 15 | To work with a higher abstraction level of this bundle, use the mapping part of 16 | [http://github.com/mmoreram/BaseBundle](https://github.com/mmoreram/BaseBundle#entity-mapping). 17 | This bundle makes the best possible integration of SimpleDoctrineMapping in your 18 | bundles. 19 | 20 | Repeat with me, *Keep it simple*. 21 | 22 | ## CompilerPass 23 | 24 | For those of you who still do not know what is a CompilerPass, try to visualize 25 | it as your last chance to configure your container. At this point you can 26 | retrieve all your parameter configuration, but you cannot build any service, this 27 | is the point where you can dynamically build and complete services. 28 | 29 | Once compiled, this container will be read-only. 30 | 31 | This CompilerPass let each bundle be responsible for its own entities, defining 32 | per each one, the class to be mapped, the path of the mapping file and the 33 | manager that will manage it. 34 | 35 | You should create your own compiler pass 36 | 37 | 38 | ``` php 39 | addEntityMapping( 67 | $container, 68 | 'default', 69 | 'TestBundle\Entity\User', 70 | '@TestBundle/Mapping/User.orm.yml', 71 | true 72 | ) 73 | ; 74 | } 75 | } 76 | ``` 77 | 78 | and add it into the Container. Like this: 79 | 80 | ``` php 81 | addCompilerPass(new MappingCompilerPass()); 105 | } 106 | } 107 | ``` 108 | 109 | And that's it. After the container compilation it will add our mapping 110 | information. No magic. 111 | 112 | 113 | ## addEntityMapping() 114 | 115 | The method *addEntityMapping()* does not offer us many options, but the necessary 116 | to be able to define the entity map in most cases. 117 | 118 | 119 | ``` php 120 | /** 121 | * Add mapping entity 122 | * 123 | * This method adds a new Driver into global MappingDriverChain with single 124 | * entity mapping information. 125 | * 126 | * $entityManagerName must be an existing entityManager. By default doctrine 127 | * creates just one common EntityManager called default, but many can be 128 | * defined with different connection information 129 | * 130 | * p.e. default 131 | * p.e. anotherEntityManager 132 | * 133 | * $entityNamespace must be an existing namespace of Entity. This value also 134 | * can be a valid and existing container parameter, with an existing 135 | * namespace of an Entity as value. 136 | * 137 | * p.e. MyBundle\Entity\User 138 | * p.e. mybundle.entity.user.class 139 | * 140 | * $entityMappingFilePath must be a path of an existing yml or xml file with 141 | * mapping information about $entityNamespace. This bundle uses Short Bundle 142 | * notation, with "@" symbol. This value also can be a valid and existing 143 | * container parameter, with a path to an existing yml or xml file as value. 144 | * 145 | * p.e. @MyBundle/Resources/config/doctrine/User.orm.yml 146 | * p.e. @MyBundle/Resources/config/doctrine/User.orm.xml 147 | * p.e. mybundle.entity.user.mapping_file_path 148 | * 149 | * Finally, $enable flag just allow you to add current mapping definition 150 | * into all Doctrine Map table, or just dismiss it. This is useful when you 151 | * want to give possibility to final user to enable or disable a mapping 152 | * class. 153 | * 154 | * @param ContainerBuilder $container Container 155 | * @param string $entityManagerName EntityManager name 156 | * @param string $entityNamespace Entity namespace 157 | * @param string $entityMappingFilePath Entity Mapping file path 158 | * @param boolean $enable Entity mapping must be included 159 | * 160 | * @return $this self Object 161 | * 162 | * @throws EntityManagerNotFound Entity Manager not found 163 | */ 164 | protected function addEntityMapping( 165 | ContainerBuilder $container, 166 | $entityManagerName, 167 | $entityNamespace, 168 | $entityMappingFilePath, 169 | $enable = true 170 | ) 171 | ``` 172 | 173 | Of course, all values are required but the last one. 174 | 175 | ## Parameters 176 | 177 | So, imagine that you are working in a public Bundle, I mean, your bundle will be 178 | installed by other projects in their vendor folder, but you want to expose your 179 | own entity model. 180 | 181 | You could think that by using this bundle you force the final user to use your 182 | model implementation in all cases, but is not. 183 | 184 | If you want to give this power to your users, if you want to expose overridable 185 | entities, you can define your model using container parameters. 186 | 187 | ``` yml 188 | parameters: 189 | 190 | # 191 | # Mapping information 192 | # 193 | test_bundle.entity.user.class: "TestBundle\Entity\User" 194 | test_bundle.entity.user.mapping_file_path: "@TestBundle/Mapping/Class.orm.yml" 195 | test_bundle.entity.user.entity_manager: default 196 | test_bundle.entity.user.enable: true 197 | ``` 198 | 199 | In that case your bundle will put at users mercy the ability to 200 | override all the required parameters by just overriding specific configuration 201 | items. 202 | 203 | You must finally create these params with your default values. 204 | 205 | ``` php 206 | addEntityMapping( 234 | $container, 235 | 'test_bundle.entity.user.entity_manager', 236 | 'test_bundle.entity.user.class', 237 | 'test_bundle.entity.user.mapping_file_path', 238 | 'test_bundle.entity.user.enable' 239 | ) 240 | ; 241 | } 242 | } 243 | ``` 244 | -------------------------------------------------------------------------------- /Tests/Miscelania/MappingAliasTest.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\Miscelania; 17 | 18 | use Mmoreram\BaseBundle\Tests\BaseFunctionalTest; 19 | use Mmoreram\BaseBundle\Tests\BaseKernel; 20 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1; 21 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\SimpleTestBundle; 22 | use Symfony\Component\HttpKernel\KernelInterface; 23 | 24 | /** 25 | * Class MappingAliasTest. 26 | */ 27 | class MappingAliasTest extends BaseFunctionalTest 28 | { 29 | /** 30 | * Get kernel. 31 | * 32 | * @return KernelInterface 33 | */ 34 | protected static function getKernel() : KernelInterface 35 | { 36 | return new BaseKernel([ 37 | new SimpleTestBundle(), 38 | ], [ 39 | 'imports' => [ 40 | ['resource' => '@BaseBundle/Resources/config/providers.yml'], 41 | ['resource' => '@BaseBundle/Resources/test/doctrine.test.yml'], 42 | ], 43 | 'doctrine' => [ 44 | 'orm' => [ 45 | 'entity_managers' => [ 46 | 'default' => [ 47 | 'connection' => 'default', 48 | ], 49 | 'alternative' => [ 50 | 'connection' => 'default', 51 | ], 52 | ], 53 | ], 54 | ], 55 | ]); 56 | } 57 | 58 | /** 59 | * Schema must be loaded in all test cases. 60 | * 61 | * @return bool 62 | */ 63 | protected static function loadSchema() : bool 64 | { 65 | return true; 66 | } 67 | 68 | /** 69 | * Test entity name lookup. 70 | */ 71 | public function testEntityNameLookup() 72 | { 73 | $class1 = new Class1(10, 'text1'); 74 | $this->save($class1); 75 | 76 | $this->find(Class1::class, 1); 77 | $this->assertNotNull($this->find('@SimpleTestBundle:Class1', 10)); 78 | $this->assertNull($this->find('@SimpleTestBundle:Class2', 1)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Tests/Miscelania/MappingCoexistingWithAutomappingTest.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\Miscelania; 17 | 18 | use Mmoreram\BaseBundle\Tests\BaseFunctionalTest; 19 | use Mmoreram\BaseBundle\Tests\BaseKernel; 20 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\ExtendedTestBundle; 21 | use Symfony\Component\HttpKernel\KernelInterface; 22 | 23 | /** 24 | * Class MappingCoexistingWithAutomappingTest. 25 | */ 26 | class MappingCoexistingWithAutomappingTest extends BaseFunctionalTest 27 | { 28 | /** 29 | * Get kernel. 30 | * 31 | * @return KernelInterface 32 | */ 33 | protected static function getKernel() : KernelInterface 34 | { 35 | return new BaseKernel([ 36 | new ExtendedTestBundle(), 37 | ], [ 38 | 'imports' => [ 39 | ['resource' => '@BaseBundle/Resources/config/providers.yml'], 40 | ['resource' => '@BaseBundle/Resources/test/framework.test.yml'], 41 | ['resource' => '@BaseBundle/Resources/test/doctrine.test.yml'], 42 | ], 43 | 'doctrine' => [ 44 | 'orm' => [ 45 | 'auto_mapping' => true, 46 | ], 47 | ], 48 | 'parameters' => [ 49 | 'testbundle.class3.class' => 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class3', 50 | 'testbundle.class3.mapping_file' => '@@ExtendedTestBundle/Mapping/Class3.orm.yaml', 51 | 'testbundle.class3.enable' => true, 52 | ], 53 | ]); 54 | } 55 | 56 | /** 57 | * Schema must be loaded in all test cases. 58 | * 59 | * @return bool 60 | */ 61 | protected static function loadSchema() : bool 62 | { 63 | return true; 64 | } 65 | 66 | /** 67 | * Testing bundle built properly. 68 | */ 69 | public function testBundle() 70 | { 71 | $this->assertNull($this->find('Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1', 1)); 72 | $this->assertNull($this->find('Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class2', 1)); 73 | $this->assertNull($this->find('Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class3', 1)); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Tests/Miscelania/MappingTest.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\Miscelania; 17 | 18 | use Mmoreram\BaseBundle\Tests\BaseFunctionalTest; 19 | use Mmoreram\BaseBundle\Tests\BaseKernel; 20 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1; 21 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class2; 22 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class3; 23 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class4; 24 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\TestBundle; 25 | use Symfony\Component\HttpKernel\KernelInterface; 26 | 27 | /** 28 | * Class MappingTest. 29 | */ 30 | class MappingTest extends BaseFunctionalTest 31 | { 32 | /** 33 | * Get kernel. 34 | * 35 | * @return KernelInterface 36 | */ 37 | protected static function getKernel() : KernelInterface 38 | { 39 | return new BaseKernel([ 40 | new TestBundle(), 41 | ], [ 42 | 'imports' => [ 43 | ['resource' => '@BaseBundle/Resources/config/providers.yml'], 44 | ['resource' => '@BaseBundle/Resources/test/doctrine.test.yml'], 45 | ], 46 | 'doctrine' => [ 47 | 'orm' => [ 48 | 'entity_managers' => [ 49 | 'default' => [ 50 | 'connection' => 'default', 51 | ], 52 | 'alternative' => [ 53 | 'connection' => 'default', 54 | ], 55 | ], 56 | ], 57 | ], 58 | ]); 59 | } 60 | 61 | /** 62 | * Schema must be loaded in all test cases. 63 | * 64 | * @return bool 65 | */ 66 | protected static function loadSchema() : bool 67 | { 68 | return true; 69 | } 70 | 71 | /** 72 | * Test all mapping data. 73 | */ 74 | public function testMappingData() 75 | { 76 | $this->assertSame( 77 | $this->getObjectManager(Class1::class), 78 | $this->get('doctrine.orm.default_entity_manager') 79 | ); 80 | 81 | $class1 = new Class1(1, 'text1'); 82 | $this->save($class1); 83 | 84 | $this->assertSame( 85 | $this->getObjectManager(Class2::class), 86 | $this->get('doctrine.orm.alternative_entity_manager') 87 | ); 88 | 89 | $class2 = new Class2(2, 'text2'); 90 | $this->save($class2); 91 | 92 | $this->assertSame( 93 | $this->getObjectManager(Class3::class), 94 | $this->get('doctrine.orm.default_entity_manager') 95 | ); 96 | 97 | $class3 = new Class3(3, 'text3'); 98 | $this->save($class3); 99 | 100 | $this->assertSame( 101 | $this->getObjectManager(Class4::class), 102 | $this->get('doctrine.orm.alternative_entity_manager') 103 | ); 104 | 105 | $class4 = new Class4(4, 'text4'); 106 | $this->save($class4); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Tests/TestBundle/CompilerPass/ExtendedMappingCompilerPass.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\CompilerPass; 17 | 18 | use Symfony\Component\DependencyInjection\ContainerBuilder; 19 | 20 | use Mmoreram\SimpleDoctrineMapping\CompilerPass\Abstracts\AbstractMappingCompilerPass; 21 | 22 | /** 23 | * Class ExtendedMappingCompilerPass. 24 | */ 25 | class ExtendedMappingCompilerPass extends AbstractMappingCompilerPass 26 | { 27 | /** 28 | * You can modify the container here before it is dumped to PHP code. 29 | * 30 | * @param ContainerBuilder $container 31 | * 32 | * @api 33 | */ 34 | public function process(ContainerBuilder $container) 35 | { 36 | $this 37 | ->addEntityMapping( 38 | $container, 39 | 'default', 40 | 'testbundle.class3.class', 41 | 'testbundle.class3.mapping_file', 42 | 'testbundle.class3.enable' 43 | ); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Tests/TestBundle/CompilerPass/MappingCompilerPass.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\CompilerPass; 17 | 18 | use Symfony\Component\DependencyInjection\ContainerBuilder; 19 | 20 | use Mmoreram\SimpleDoctrineMapping\CompilerPass\Abstracts\AbstractMappingCompilerPass; 21 | 22 | /** 23 | * Class MappingCompilerPass. 24 | */ 25 | class MappingCompilerPass extends AbstractMappingCompilerPass 26 | { 27 | /** 28 | * You can modify the container here before it is dumped to PHP code. 29 | * 30 | * @param ContainerBuilder $container 31 | * 32 | * @api 33 | */ 34 | public function process(ContainerBuilder $container) 35 | { 36 | $this 37 | ->addEntityMapping( 38 | $container, 39 | 'default', 40 | 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1', 41 | '@TestBundle/Mapping/Class1.orm.yml' 42 | ) 43 | ->addEntityMapping( 44 | $container, 45 | 'alternative', 46 | 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class2', 47 | '@TestBundle/Mapping/Class2.custom.orm.yml', 48 | true 49 | ) 50 | ->addEntityMapping( 51 | $container, 52 | 'default', 53 | 'testbundle.class3.class', 54 | 'testbundle.class3.mapping_file', 55 | 'testbundle.class3.enable' 56 | ) 57 | ->addEntityMapping( 58 | $container, 59 | 'testbundle.class4.entity_manager', 60 | 'testbundle.class4.class', 61 | 'testbundle.class4.mapping_file' 62 | ) 63 | ->addEntityMapping( 64 | $container, 65 | 'notexistingone', 66 | 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\NonExisting', 67 | '@TestBundle/Mapping/NonExisting.orm.yml', 68 | false 69 | ); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Tests/TestBundle/CompilerPass/SimpleMappingCompilerPass.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\CompilerPass; 17 | 18 | use Symfony\Component\DependencyInjection\ContainerBuilder; 19 | 20 | use Mmoreram\SimpleDoctrineMapping\CompilerPass\Abstracts\AbstractMappingCompilerPass; 21 | 22 | /** 23 | * Class SimpleMappingCompilerPass. 24 | */ 25 | class SimpleMappingCompilerPass extends AbstractMappingCompilerPass 26 | { 27 | /** 28 | * You can modify the container here before it is dumped to PHP code. 29 | * 30 | * @param ContainerBuilder $container 31 | * 32 | * @api 33 | */ 34 | public function process(ContainerBuilder $container) 35 | { 36 | $this 37 | ->addEntityMapping( 38 | $container, 39 | 'default', 40 | 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1', 41 | '@SimpleTestBundle/Mapping/Class1.orm.yml' 42 | ) 43 | ->addEntityMapping( 44 | $container, 45 | 'alternative', 46 | 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class2', 47 | '@SimpleTestBundle/Mapping/Class2.custom.orm.yml', 48 | true 49 | ); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Tests/TestBundle/DependencyInjection/TestExtension.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\DependencyInjection; 17 | 18 | use Mmoreram\BaseBundle\DependencyInjection\BaseExtension; 19 | 20 | /** 21 | * Class TestExtension. 22 | */ 23 | class TestExtension extends BaseExtension 24 | { 25 | /** 26 | * Returns the recommended alias to use in XML. 27 | * 28 | * This alias is also the mandatory prefix to use when using YAML. 29 | * 30 | * @return string The alias 31 | */ 32 | public function getAlias() 33 | { 34 | return 'test'; 35 | } 36 | 37 | /** 38 | * Load Parametrization definition. 39 | * 40 | * return array( 41 | * 'parameter1' => $config['parameter1'], 42 | * 'parameter2' => $config['parameter2'], 43 | * ... 44 | * ); 45 | * 46 | * @param array $config Bundles config values 47 | * 48 | * @return array 49 | */ 50 | protected function getParametrizationValues(array $config) : array 51 | { 52 | return [ 53 | 'testbundle.class3.class' => 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class3', 54 | 'testbundle.class3.mapping_file' => '@TestBundle/Mapping/Class3.orm.yaml', 55 | 'testbundle.class3.enable' => true, 56 | 57 | 'testbundle.class4.class' => 'Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class4', 58 | 'testbundle.class4.mapping_file' => '@TestBundle/Mapping/Class4.orm.xml', 59 | 'testbundle.class4.entity_manager' => 'alternative', 60 | ]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Tests/TestBundle/Entity/Class1.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity; 17 | 18 | /** 19 | * Class Class1. 20 | */ 21 | class Class1 22 | { 23 | /** 24 | * @var int 25 | * 26 | * Entity id 27 | */ 28 | protected $id; 29 | 30 | /** 31 | * @var string 32 | * 33 | * Code 34 | */ 35 | protected $code; 36 | 37 | /** 38 | * Class1 constructor. 39 | * 40 | * @param int $id 41 | * @param string $code 42 | */ 43 | public function __construct( 44 | int $id, 45 | string $code 46 | ) { 47 | $this->id = $id; 48 | $this->code = $code; 49 | } 50 | 51 | /** 52 | * Get Id. 53 | * 54 | * @return int 55 | */ 56 | public function getId() : int 57 | { 58 | return $this->id; 59 | } 60 | 61 | /** 62 | * Get Code. 63 | * 64 | * @return string 65 | */ 66 | public function getCode() : string 67 | { 68 | return $this->code; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Tests/TestBundle/Entity/Class2.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity; 17 | 18 | /** 19 | * Class Class2. 20 | */ 21 | class Class2 22 | { 23 | /** 24 | * @var int 25 | * 26 | * Entity id 27 | */ 28 | protected $id; 29 | 30 | /** 31 | * @var string 32 | * 33 | * Name 34 | */ 35 | protected $name; 36 | 37 | /** 38 | * Class2 constructor. 39 | * 40 | * @param int $id 41 | * @param string $name 42 | */ 43 | public function __construct( 44 | int $id, 45 | string $name 46 | ) { 47 | $this->id = $id; 48 | $this->name = $name; 49 | } 50 | 51 | /** 52 | * Get Id. 53 | * 54 | * @return int 55 | */ 56 | public function getId(): int 57 | { 58 | return $this->id; 59 | } 60 | 61 | /** 62 | * Get Name. 63 | * 64 | * @return string 65 | */ 66 | public function getName(): string 67 | { 68 | return $this->name; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Tests/TestBundle/ExtendedTestBundle.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle; 17 | 18 | use Mmoreram\BaseBundle\BaseBundle; 19 | 20 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\CompilerPass\ExtendedMappingCompilerPass; 21 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 22 | use Symfony\Component\HttpKernel\KernelInterface; 23 | 24 | /** 25 | * Class ExtendedTestBundle. 26 | */ 27 | final class ExtendedTestBundle extends BaseBundle 28 | { 29 | /** 30 | * Return a CompilerPass instance array. 31 | * 32 | * @return CompilerPassInterface[] 33 | */ 34 | public function getCompilerPasses() 35 | { 36 | return [ 37 | new ExtendedMappingCompilerPass(), 38 | ]; 39 | } 40 | 41 | /** 42 | * Create instance of current bundle, and return dependent bundle namespaces. 43 | * 44 | * @return array Bundle instances 45 | */ 46 | public static function getBundleDependencies(KernelInterface $kernel) 47 | { 48 | return [ 49 | 'Symfony\Bundle\FrameworkBundle\FrameworkBundle', 50 | 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle', 51 | 'Mmoreram\BaseBundle\BaseBundle', 52 | ]; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Tests/TestBundle/Mapping/Class1.orm.yml: -------------------------------------------------------------------------------- 1 | Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1: 2 | type: entity 3 | table: class1 4 | id: 5 | id: 6 | type: integer 7 | fields: 8 | code: 9 | column: code 10 | type: string 11 | length: 255 12 | -------------------------------------------------------------------------------- /Tests/TestBundle/Mapping/Class2.custom.orm.yml: -------------------------------------------------------------------------------- 1 | Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class2: 2 | type: entity 3 | table: class2 4 | id: 5 | id: 6 | type: integer 7 | fields: 8 | name: 9 | column: name 10 | type: string 11 | length: 255 12 | -------------------------------------------------------------------------------- /Tests/TestBundle/Mapping/Class3.orm.yaml: -------------------------------------------------------------------------------- 1 | Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace\Class3: 2 | type: entity 3 | table: class3 4 | id: 5 | id: 6 | type: integer 7 | fields: 8 | phone: 9 | column: phone 10 | type: string 11 | length: 255 -------------------------------------------------------------------------------- /Tests/TestBundle/Mapping/Class4.orm.xml: -------------------------------------------------------------------------------- 1 | 7 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Tests/TestBundle/MyOtherEntityNamespace/Class3.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace; 17 | 18 | /** 19 | * Class Class3. 20 | */ 21 | class Class3 22 | { 23 | /** 24 | * @var int 25 | * 26 | * Entity id 27 | */ 28 | protected $id; 29 | 30 | /** 31 | * @var string 32 | * 33 | * Phone 34 | */ 35 | protected $phone; 36 | 37 | /** 38 | * Class3 constructor. 39 | * 40 | * @param int $id 41 | * @param string $phone 42 | */ 43 | public function __construct( 44 | int $id, 45 | string $phone 46 | ) { 47 | $this->id = $id; 48 | $this->phone = $phone; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Tests/TestBundle/MyOtherEntityNamespace/Class4.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\MyOtherEntityNamespace; 17 | 18 | /** 19 | * Class Class4. 20 | */ 21 | class Class4 22 | { 23 | /** 24 | * @var int 25 | * 26 | * Entity id 27 | */ 28 | protected $id; 29 | 30 | /** 31 | * @var string 32 | * 33 | * Lastname 34 | */ 35 | protected $lastname; 36 | 37 | /** 38 | * Class4 constructor. 39 | * 40 | * @param int $id 41 | * @param string $lastname 42 | */ 43 | public function __construct( 44 | int $id, 45 | string $lastname 46 | ) { 47 | $this->id = $id; 48 | $this->lastname = $lastname; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Tests/TestBundle/Resources/config/doctrine/Class1.orm.yml: -------------------------------------------------------------------------------- 1 | Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class1: 2 | type: entity 3 | table: class1 4 | id: 5 | id: 6 | type: integer 7 | fields: 8 | code: 9 | column: code 10 | type: string 11 | length: 255 12 | -------------------------------------------------------------------------------- /Tests/TestBundle/Resources/config/doctrine/Class2.orm.yml: -------------------------------------------------------------------------------- 1 | Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\Entity\Class2: 2 | type: entity 3 | table: class2 4 | id: 5 | id: 6 | type: integer 7 | fields: 8 | name: 9 | column: name 10 | type: string 11 | length: 255 12 | -------------------------------------------------------------------------------- /Tests/TestBundle/SimpleTestBundle.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle; 17 | 18 | use Mmoreram\BaseBundle\BaseBundle; 19 | 20 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\CompilerPass\SimpleMappingCompilerPass; 21 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 22 | use Symfony\Component\HttpKernel\KernelInterface; 23 | 24 | /** 25 | * Class SimpleTestBundle. 26 | */ 27 | final class SimpleTestBundle extends BaseBundle 28 | { 29 | /** 30 | * Return a CompilerPass instance array. 31 | * 32 | * @return CompilerPassInterface[] 33 | */ 34 | public function getCompilerPasses() 35 | { 36 | return [ 37 | new SimpleMappingCompilerPass(), 38 | ]; 39 | } 40 | 41 | /** 42 | * Create instance of current bundle, and return dependent bundle namespaces. 43 | * 44 | * @return array Bundle instances 45 | */ 46 | public static function getBundleDependencies(KernelInterface $kernel) 47 | { 48 | return [ 49 | 'Symfony\Bundle\FrameworkBundle\FrameworkBundle', 50 | 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle', 51 | 'Mmoreram\BaseBundle\BaseBundle', 52 | ]; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Tests/TestBundle/TestBundle.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | 14 | declare(strict_types=1); 15 | 16 | namespace Mmoreram\SimpleDoctrineMapping\Tests\TestBundle; 17 | 18 | use Mmoreram\BaseBundle\BaseBundle; 19 | 20 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\CompilerPass\MappingCompilerPass; 21 | use Mmoreram\SimpleDoctrineMapping\Tests\TestBundle\DependencyInjection\TestExtension; 22 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 23 | use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; 24 | use Symfony\Component\HttpKernel\KernelInterface; 25 | 26 | /** 27 | * Class TestBundle. 28 | */ 29 | final class TestBundle extends BaseBundle 30 | { 31 | /** 32 | * Return a CompilerPass instance array. 33 | * 34 | * @return CompilerPassInterface[] 35 | */ 36 | public function getCompilerPasses() 37 | { 38 | return [ 39 | new MappingCompilerPass(), 40 | ]; 41 | } 42 | 43 | /** 44 | * Returns the bundle's container extension. 45 | * 46 | * @return ExtensionInterface|null The container extension 47 | * 48 | * @throws \LogicException 49 | */ 50 | public function getContainerExtension() 51 | { 52 | return new TestExtension(); 53 | } 54 | 55 | /** 56 | * Create instance of current bundle, and return dependent bundle namespaces. 57 | * 58 | * @return array Bundle instances 59 | */ 60 | public static function getBundleDependencies(KernelInterface $kernel) 61 | { 62 | return [ 63 | 'Symfony\Bundle\FrameworkBundle\FrameworkBundle', 64 | 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle', 65 | 'Mmoreram\BaseBundle\BaseBundle', 66 | ]; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mmoreram/simple-doctrine-mapping", 3 | "description": "Simple Doctrine Mapping ", 4 | "keywords": [ 5 | "doctrine", 6 | "mapping", 7 | "mmoreram" 8 | ], 9 | "homepage": "https://github.com/mmoreram/SimpleDoctrineMapping", 10 | "type": "library", 11 | "license": "MIT", 12 | "support": { 13 | "email": "yuhu@mmoreram.com", 14 | "forum": "https://gitter.im/mmoreram/SimpleDoctrineMapping", 15 | "source": "https://github.com/mmoreram/SimpleDoctrineMapping", 16 | "issues": "https://github.com/mmoreram/SimpleDoctrineMapping/issues" 17 | }, 18 | "authors": [ 19 | { 20 | "name": "Marc Morera", 21 | "email": "yuhu@mmoreram.com" 22 | }, 23 | { 24 | "name": "SimpleDoctrineMapping Community", 25 | "homepage": "https://github.com/mmoreram/SimpleDoctrineMapping/contributors" 26 | } 27 | ], 28 | "require": { 29 | "php": "^7.1", 30 | "doctrine/common": "^2.6", 31 | "doctrine/orm": "^2.5", 32 | "symfony/http-kernel": "^3.2", 33 | "symfony/dependency-injection": "^3.2" 34 | }, 35 | "require-dev": { 36 | "symfony/framework-bundle": "^3.2", 37 | "doctrine/doctrine-bundle": "^1.6.4", 38 | "mmoreram/base-bundle": "dev-master", 39 | "friendsofphp/php-cs-fixer": "1.12.4", 40 | "mmoreram/php-formatter": "^1.2.0", 41 | "phpunit/phpunit": "^5.6.4" 42 | }, 43 | "autoload": { 44 | "psr-4": { 45 | "Mmoreram\\SimpleDoctrineMapping\\": "" 46 | } 47 | }, 48 | "scripts": { 49 | "fix-code": [ 50 | "vendor/bin/php-cs-fixer fix --config-file=.php_cs", 51 | "vendor/bin/php-formatter f:h:f .", 52 | "vendor/bin/php-formatter f:s:f .", 53 | "vendor/bin/php-formatter f:u:s ." 54 | ], 55 | "test": "vendor/bin/phpunit" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | 15 | 16 | ./Tests 17 | 18 | 19 | 20 | 21 | 22 | ./ 23 | 24 | ./Tests/ 25 | ./vendor/ 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | --------------------------------------------------------------------------------