Skip to content
Snippets Groups Projects
Select Git revision
  • d07f9f7dcf4c6aed3345e8e2c5c5fb45530bb7bb
  • master default protected
  • ll-workflow
  • alc-scindage-donnees-pj
  • b24
  • FJ_LL_Tbl_Contrat
  • alc-docker-node
  • ll-apiplatform
  • php84
  • ll-rgpd
  • b23
  • alc-filtre-type-intervenant
  • ll-sans-mdb5
  • formules-ancienne-infra
  • ll-formules
  • alc-intervenant-dmep
  • ll-suppr-v_vol-s
  • b20
  • ll-postgresql
  • b23.0.1
  • b22
  • 24.8
  • 24.7
  • 24.6
  • 24.5
  • 24.4
  • 24.3
  • 24.2
  • 24.1
  • 24.0
  • 23.15
  • 24.0-beta19
  • 24.0-beta18
  • 24.0-beta17
  • 24.0-beta16
  • 24.0-beta15
  • 24.0-beta14
  • 24.0-beta13
  • 23.14
  • 24.0-beta12
  • 24.0-beta11
41 results

ServiceAwareTrait.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ServiceAwareTrait.php 1.25 KiB
    <?php
    
    namespace <namespace>;
    
    use <targetFullClass>;
    use Application\Module;
    use RuntimeException;
    
    /**
     * Description of <class>
     *
     * @author UnicaenCode
     */
    trait <class>
    {
        /**
         * @var <targetClass>
         */
        private $<variable>;
    
    
    
    
    
        /**
         * @param <targetClass> $<variable>
         * @return self
         */
        public function set<method>( <targetClass> $<variable> )
        {
            $this-><variable> = $<variable>;
            return $this;
        }
    
    
    
        /**
         * @return <targetClass>
         * @throws RuntimeException
         */
        public function get<method>()
        {
            if (empty($this-><variable>)){
                $serviceLocator = Module::$serviceLocator;
                if (! $serviceLocator) {
                    if (!method_exists($this, 'getServiceLocator')) {
                        throw new RuntimeException('La classe ' . get_class($this) . ' n\'a pas accès au ServiceLocator.');
                    }
    
                    $serviceLocator = $this->getServiceLocator();
                    if (method_exists($serviceLocator, 'getServiceLocator')) {
                        $serviceLocator = $serviceLocator->getServiceLocator();
                    }
                }
                $this-><variable> = $serviceLocator->get('<name>');
            }
            return $this-><variable>;
        }
    }