Commits

Arkadiusz Dzięgiel committed 1967b0b

changed services ids,
added PropelClassFinder

Comments (0)

Files changed (3)

DependencyInjection/GlorpenPropelExtension.php

     }
     
     public function setupOverrider(array $config, ContainerBuilder $container){
-    	$container->getDefinition("glorpen.propel.om_overrider")
+    	$container->getDefinition("glorpen.propel.listeners.om_overrider")
     		->addArgument($config['extended_models']);
     }
     

Resources/config/services.xml

 
     <parameters>
         <parameter key="glorpen.propel.event.dispatcher.class">Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher</parameter>
-        <parameter key="glorpen.propel.om_overrider.class">Glorpen\Propel\PropelBundle\Services\OMClassOverrider</parameter>
-        <parameter key="glorpen.propel.container_aware.class">Glorpen\Propel\PropelBundle\Services\ContainerAwareModel</parameter>
-        <parameter key="glorpen.propel.transaction_aware.class">Glorpen\Propel\PropelBundle\Services\TransactionLifeCycle</parameter>
+        <parameter key="glorpen.propel.listeners.om_overrider.class">Glorpen\Propel\PropelBundle\Services\OMClassOverrider</parameter>
+        <parameter key="glorpen.propel.listeners.container_aware.class">Glorpen\Propel\PropelBundle\Services\ContainerAwareModel</parameter>
+        <parameter key="glorpen.propel.listeners.transaction_aware.class">Glorpen\Propel\PropelBundle\Services\TransactionLifeCycle</parameter>
+        <parameter key="glorpen.propel.class_finder.class">Glorpen\Propel\PropelBundle\Services\PropelClassFinder</parameter>
     </parameters>
 
     <services>
 		<service id="glorpen.propel.event.dispatcher" class="%glorpen.propel.event.dispatcher.class%">
             <argument type="service" id="service_container" />
         </service>
-        <service id="glorpen.propel.om_overrider" class="%glorpen.propel.om_overrider.class%">
+        <service id="glorpen.propel.listeners.om_overrider" class="%glorpen.propel.listeners.om_overrider.class%">
         	<tag name="propel.event" method="onDetectionRequest" event="om.detect" />
         </service>
-        <service id="glorpen.propel.container_aware" class="%glorpen.propel.container_aware.class%">
+        <service id="glorpen.propel.listeners.container_aware" class="%glorpen.propel.listeners.container_aware.class%">
         	<argument type="service" id="service_container" />
         	<tag name="propel.event" method="onModelConstruct" event="model.construct" />
         </service>
-        <service id="glorpen.propel.transaction_aware" class="%glorpen.propel.transaction_aware.class%">
+        <service id="glorpen.propel.listeners.transaction_aware" class="%glorpen.propel.listeners.transaction_aware.class%">
         	<tag name="propel.event" />
         </service>
+        <service id="glorpen.propel.class_finder" class="%glorpen.propel.class_finder.class%" />
     </services>
 </container>

Services/PropelClassFinder.php

+<?php
+namespace Glorpen\Propel\PropelBundle\Services;
+
+/**
+ * @author Arkadiusz Dzięgiel
+ */
+class PropelClassFinder {
+	
+	public function findQueryClass($modelClass){
+		//the propel way, like in \PropelQuery
+		$queryClass = $modelClass . 'Query';
+		if (class_exists($queryClass)) return $queryClass;
+		
+		//try with peer
+		if(class_exists($modelClass) && defined($modelClass.'::PEER')){
+			$peerClass = $modelClass::PEER;
+			if(!$peerClass::getTableMap()->isSingleTableInheritance()){
+				$queryClass = substr($peerClass,0,-4).'Query';
+				if (class_exists($queryClass)) return $queryClass;
+			} else {
+				//single inheritance
+				$peerRef = new \ReflectionClass($peerClass);
+				$modelRef = new \ReflectionClass($modelClass);
+				
+				foreach($peerRef->getConstants() as $name=>$value){
+					if(strncmp('CLASSNAME_', $name, 10) == 0 && $modelRef->isSubclassOf($value)){
+						$queryClass = $value.'Query';
+						if (class_exists($queryClass)) return $queryClass;
+						break;
+					}
+				}
+			}
+		}
+		
+		throw new \LogicException('Can\'t find query class '.$queryClass);
+	}
+	
+	public function findPeerClass($modelClass){
+		return $modelClass::PEER;
+	}
+	
+	public function findModelClass($peerClass){
+		return $peerClass::getOMClass();
+	}
+	
+	// in case when getOMClass is changed
+	public function findRealModelClass($modelClass){
+		return $this->findModelClass($this->findPeerClass($modelClass));
+	}
+	
+}