>  기사  >  php教程  >  Zend Framework 튜토리얼의 애플리케이션 사용 예에 ​​대한 자세한 설명

Zend Framework 튜토리얼의 애플리케이션 사용 예에 ​​대한 자세한 설명

高洛峰
高洛峰원래의
2016-12-27 14:19:021234검색

이 기사의 예에서는 Zend Framework 튜토리얼의 애플리케이션 사용법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

Zend_Application은 Zend Framework의 핵심 구성 요소입니다. Zend_Application은 Zend Framework 애플리케이션에 대한 기본 기능을 제공하며 프로그램의 진입점입니다. 주요 기능은 PHP 환경 로드 및 구성(자동 로드 포함)과 애플리케이션 부팅의 두 가지입니다.

일반적으로 Zend_Application 생성자는 구성 옵션을 통해 구성되지만 사용자 지정 방법을 사용하여 완전히 구성할 수도 있습니다. 아래에는 두 가지 사용 사례가 있습니다.

Zend_Application 구성 옵션

생성자:

/**
 * Constructor
 *
 * Initialize application. Potentially initializes include_paths, PHP
 * settings, and bootstrap class.
 *
 * @param string          $environment
 * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
 * @throws Zend_Application_Exception When invalid options are provided
 * @return void
 */
public function __construct($environment, $options = null)
{
  $this->_environment = (string) $environment;
  require_once 'Zend/Loader/Autoloader.php';
  $this->_autoloader = Zend_Loader_Autoloader::getInstance();
  if (null !== $options) {
    if (is_string($options)) {
      $options = $this->_loadConfig($options);
    } elseif ($options instanceof Zend_Config) {
      $options = $options->toArray();
    } elseif (!is_array($options)) {
      throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
    }
    $this->setOptions($options);
  }
}

Zend_Application 구성 방법

1. 구성 파일 사용
2. >

공통 구성 옵션

Zend Framework 튜토리얼의 애플리케이션 사용 예에 ​​대한 자세한 설명

참고:

옵션 이름은 대소문자를 구분하지 않습니다.

Zend_응용 방법

Zend Framework 튜토리얼의 애플리케이션 사용 예에 ​​대한 자세한 설명

Zend Framework 튜토리얼의 애플리케이션 사용 예에 ​​대한 자세한 설명

Zend Framework 튜토리얼의 애플리케이션 사용 예에 ​​대한 자세한 설명

구성 예:

기본값:

// Create application, bootstrap, and run
$application = new Zend_Application(
  APPLICATION_ENV,
  APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
      ->run();
소스 코드

<?php
class Zend_Application
{  /**
   * Constructor
   *
   * Initialize application. Potentially initializes include_paths, PHP
   * settings, and bootstrap class.
   *
   * @param string          $environment
   * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
   * @throws Zend_Application_Exception When invalid options are provided
   * @return void
   */
  public function __construct($environment, $options = null)
  {
    $this->_environment = (string) $environment;
    require_once &#39;Zend/Loader/Autoloader.php&#39;;
    $this->_autoloader = Zend_Loader_Autoloader::getInstance();
    if (null !== $options) {
      if (is_string($options)) {
        $options = $this->_loadConfig($options);
      } elseif ($options instanceof Zend_Config) {
        $options = $options->toArray();
      } elseif (!is_array($options)) {
        throw new Zend_Application_Exception(&#39;Invalid options provided; must be location of config file, a config object, or an array&#39;);
      }
      $this->setOptions($options);
    }
  }
  /**
   * Retrieve current environment
   *
   * @return string
   */
  public function getEnvironment()
  {
    return $this->_environment;
  }
  /**
   * Retrieve autoloader instance
   *
   * @return Zend_Loader_Autoloader
   */
  public function getAutoloader()
  {
    return $this->_autoloader;
  }
  /**
   * Set application options
   *
   * @param array $options
   * @throws Zend_Application_Exception When no bootstrap path is provided
   * @throws Zend_Application_Exception When invalid bootstrap information are provided
   * @return Zend_Application
   */
  public function setOptions(array $options)
  {
    if (!empty($options[&#39;config&#39;])) {
      if (is_array($options[&#39;config&#39;])) {
        $_options = array();
        foreach ($options[&#39;config&#39;] as $tmp) {
          $_options = $this->mergeOptions($_options, $this->_loadConfig($tmp));
        }
        $options = $this->mergeOptions($_options, $options);
      } else {
        $options = $this->mergeOptions($this->_loadConfig($options[&#39;config&#39;]), $options);
      }
    }
    $this->_options = $options;
    $options = array_change_key_case($options, CASE_LOWER);
    $this->_optionKeys = array_keys($options);
    if (!empty($options[&#39;phpsettings&#39;])) {
      $this->setPhpSettings($options[&#39;phpsettings&#39;]);
    }
    if (!empty($options[&#39;includepaths&#39;])) {
      $this->setIncludePaths($options[&#39;includepaths&#39;]);
    }
    if (!empty($options[&#39;autoloadernamespaces&#39;])) {
      $this->setAutoloaderNamespaces($options[&#39;autoloadernamespaces&#39;]);
    }
    if (!empty($options[&#39;autoloaderzfpath&#39;])) {
      $autoloader = $this->getAutoloader();
      if (method_exists($autoloader, &#39;setZfPath&#39;)) {
        $zfPath  = $options[&#39;autoloaderzfpath&#39;];
        $zfVersion = !empty($options[&#39;autoloaderzfversion&#39;])
              ? $options[&#39;autoloaderzfversion&#39;]
              : &#39;latest&#39;;
        $autoloader->setZfPath($zfPath, $zfVersion);
      }
    }
    if (!empty($options[&#39;bootstrap&#39;])) {
      $bootstrap = $options[&#39;bootstrap&#39;];
      if (is_string($bootstrap)) {
        $this->setBootstrap($bootstrap);
      } elseif (is_array($bootstrap)) {
        if (empty($bootstrap[&#39;path&#39;])) {
          throw new Zend_Application_Exception(&#39;No bootstrap path provided&#39;);
        }
        $path = $bootstrap[&#39;path&#39;];
        $class = null;
        if (!empty($bootstrap[&#39;class&#39;])) {
          $class = $bootstrap[&#39;class&#39;];
        }
        $this->setBootstrap($path, $class);
      } else {
        throw new Zend_Application_Exception(&#39;Invalid bootstrap information provided&#39;);
      }
    }
    return $this;
  }
  /**
   * Retrieve application options (for caching)
   *
   * @return array
   */
  public function getOptions()
  {
    return $this->_options;
  }
  /**
   * Is an option present?
   *
   * @param string $key
   * @return bool
   */
  public function hasOption($key)
  {
    return in_array(strtolower($key), $this->_optionKeys);
  }
  /**
   * Retrieve a single option
   *
   * @param string $key
   * @return mixed
   */
  public function getOption($key)
  {
  }
  /**
   * Merge options recursively
   *
   * @param array $array1
   * @param mixed $array2
   * @return array
   */
  public function mergeOptions(array $array1, $array2 = null)
  {
    if (is_array($array2)) {
      foreach ($array2 as $key => $val) {
        if (is_array($array2[$key])) {
          $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
                 ? $this->mergeOptions($array1[$key], $array2[$key])
                 : $array2[$key];
        } else {
          $array1[$key] = $val;
        }
      }
    }
    return $array1;
  }
  /**
   * Set PHP configuration settings
   *
   * @param array $settings
   * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
   * @return Zend_Application
   */
  public function setPhpSettings(array $settings, $prefix = &#39;&#39;)
  {
    foreach ($settings as $key => $value) {
      $key = empty($prefix) ? $key : $prefix . $key;
      if (is_scalar($value)) {
        ini_set($key, $value);
      } elseif (is_array($value)) {
        $this->setPhpSettings($value, $key . &#39;.&#39;);
      }
    }
    return $this;
  }
  /**
   * Set include path
   *
   * @param array $paths
   * @return Zend_Application
   */
  public function setIncludePaths(array $paths)
  {
    $path = implode(PATH_SEPARATOR, $paths);
    set_include_path($path . PATH_SEPARATOR . get_include_path());
    return $this;
  }
  /**
   * Set autoloader namespaces
   *
   * @param array $namespaces
   * @return Zend_Application
   */
  public function setAutoloaderNamespaces(array $namespaces)
  {
    $autoloader = $this->getAutoloader();
    foreach ($namespaces as $namespace) {
      $autoloader->registerNamespace($namespace);
    }
    return $this;
  }
  /**
   * Set bootstrap path/class
   *
   * @param string $path
   * @param string $class
   * @return Zend_Application
   */
  public function setBootstrap($path, $class = null)
  {
    // setOptions() can potentially send a null value; specify default
    // here
    if (null === $class) {
      $class = &#39;Bootstrap&#39;;
    }
    if (!class_exists($class, false)) {
      require_once $path;
      if (!class_exists($class, false)) {
        throw new Zend_Application_Exception(&#39;Bootstrap class not found&#39;);
      }
    }
    $this->_bootstrap = new $class($this);
    if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
      throw new Zend_Application_Exception(&#39;Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper&#39;);
    }
    return $this;
  }
  /**
   * Get bootstrap object
   *
   * @return Zend_Application_Bootstrap_BootstrapAbstract
   */
  public function getBootstrap()
  {
    if (null === $this->_bootstrap) {
      $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
    }
    return $this->_bootstrap;
  }
  /**
   * Bootstrap application
   *
   * @param null|string|array $resource
   * @return Zend_Application
   */
  public function bootstrap($resource = null)
  {
    $this->getBootstrap()->bootstrap($resource);
    return $this;
  }
  /**
   * Run the application
   *
   * @return void
   */
  public function run()
  {
    $this->getBootstrap()->run();
  }
  /**
   * Load configuration file of options
   *
   * @param string $file
   * @throws Zend_Application_Exception When invalid configuration file is provided
   * @return array
   */
  protected function _loadConfig($file)
  {
    $environment = $this->getEnvironment();
    $suffix   = pathinfo($file, PATHINFO_EXTENSION);
    $suffix   = ($suffix === &#39;dist&#39;)
           ? pathinfo(basename($file, ".$suffix"), PATHINFO_EXTENSION)
           : $suffix;
    switch (strtolower($suffix)) {
      case &#39;ini&#39;:
        $config = new Zend_Config_Ini($file, $environment);
        break;
      case &#39;xml&#39;:
        $config = new Zend_Config_Xml($file, $environment);
        break;
      case &#39;json&#39;:
        $config = new Zend_Config_Json($file, $environment);
        break;
      case &#39;yaml&#39;:
      case &#39;yml&#39;:
        $config = new Zend_Config_Yaml($file, $environment);
        break;
      case &#39;php&#39;:
      case &#39;inc&#39;:
        $config = include $file;
        if (!is_array($config)) {
          throw new Zend_Application_Exception(&#39;Invalid configuration file provided; PHP file does not return array value&#39;);
        }
        return $config;
        break;
      default:
        throw new Zend_Application_Exception(&#39;Invalid configuration file provided; unknown config type&#39;);
    }
    return $config->toArray();
  }
}
이 기사가 PHP 프로그래밍에 종사하는 모든 사람에게 도움이 되기를 바랍니다.

Zend Framework 튜토리얼 및 관련 기사에 포함된 애플리케이션 사용 예에 ​​대한 자세한 설명을 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.