/**
  * Initializes installer: creation of all required objects and validating them
  *
  * {@inheritDoc}
  */
 public function __construct(IOInterface $io, Composer $composer, $type = 'library')
 {
     self::$__io = $io;
     self::$__composer = $composer;
     self::$__type = $type;
     $package = self::$__composer->getPackage();
     $config = self::$__composer->getConfig();
     $extra = $package->getExtra();
     // Config
     if (isset($extra['assets-config-class'])) {
         $config_class = $extra['assets-config-class'];
         if ($this->validateConfig($config_class)) {
             Config::load($config_class);
             Config::overload($extra);
         } else {
             self::$__io->write(sprintf('<warning>AssetsManager Notice: skipping configuration class "%s": class not found!</warning>', $config_class));
         }
     }
     // Installer
     $installer_class = Config::get('assets-package-installer-class');
     if (!empty($installer_class)) {
         if (!$this->validateInstaller($installer_class)) {
             self::$__io->write(sprintf('<warning>AssetsManager Notice: skipping assets installer class "%s": class not found!</warning>', $installer_class));
             $installer_class = Config::getInternal('assets-package-installer-class');
         }
         if (class_exists($installer_class)) {
             $interfaces = class_implements($installer_class);
             $installer_interface = Config::getInternal('assets-package-installer-interface');
             if (in_array($installer_interface, $interfaces)) {
                 self::$__installer = new $installer_class(self::$__io, self::$__composer, self::$__type);
             } else {
                 Error::thrower(sprintf('Assets package installer class "%s" must implement interface "%s"!', $installer_class, $installer_interface), '\\DomainException', __CLASS__, __METHOD__, __LINE__);
             }
         } else {
             Error::thrower(sprintf('Assets package installer class "%s" not found!', $installer_class), '\\DomainException', __CLASS__, __METHOD__, __LINE__);
         }
     } else {
         Error::thrower('Assets package installer is not defined!', '\\Exception', __CLASS__, __METHOD__, __LINE__);
     }
     // AutoloadGenerator
     $autoload_class = Config::get('assets-autoload-generator-class');
     if (!empty($autoload_class)) {
         if (!$this->validateAutoloadGenerator($autoload_class)) {
             self::$__io->write(sprintf('<warning>AssetsManager Notice: skipping autoload generator class "%s": class not found!</warning>', $autoload_class));
             $autoload_class = Config::getInternal('assets-autoload-generator-class');
         }
         if (class_exists($autoload_class)) {
             $parents = class_parents($autoload_class);
             $autoload_abstract = Config::getInternal('assets-autoload-generator-abstract');
             if (in_array($autoload_abstract, $parents)) {
                 self::$__autoloader = $autoload_class::getInstance(self::$__installer);
             } else {
                 Error::thrower(sprintf('Assets autoload generator class "%s" must extend abstract class "%s"!', $autoload_class, $autoload_abstract), '\\DomainException', __CLASS__, __METHOD__, __LINE__);
             }
         } else {
             Error::thrower(sprintf('Assets autoload generator class "%s" not found!', $installer_class), '\\DomainException', __CLASS__, __METHOD__, __LINE__);
         }
     } else {
         Error::thrower('Assets autoload generator is not defined!', '\\Exception', __CLASS__, __METHOD__, __LINE__);
     }
 }
Esempio n. 2
0
 /**
  * Initializing a new loader populating all paths & packages
  *
  * @param   string  $root_dir       The project package root directory
  * @param   string  $assets_dir     The project package assets directory, related from `$root_dir`
  * @param   string  $document_root  The project assets root directory to build web accessible assets paths
  * @return  void
  * @throws  \Exception : any caught exception
  * @throws  \Exception if the package's `ASSETS_DB_FILENAME` was not found
  */
 public function init($root_dir = null, $assets_dir = null, $document_root = null)
 {
     try {
         $this->setRootDirectory(!is_null($root_dir) ? $root_dir : __DIR__ . '/../../../../../');
         $composer = $this->getRootDirectory() . '/' . Config::getInternal('composer-db');
         $vendor_dir = Config::get('vendor-dir');
         if (file_exists($composer)) {
             $json_package = json_decode(file_get_contents($composer), true);
             if (isset($json_package['config']) && isset($json_package['config']['vendor-dir'])) {
                 $vendor_dir = $json_package['config']['vendor-dir'];
             }
         } else {
             throw new \Exception(sprintf('Composer json "%s" not found!', $composer));
         }
         $this->setVendorDirectory($vendor_dir);
         $extra = isset($json_package['extra']) ? $json_package['extra'] : array();
         if (isset($extra['assets-config-class'])) {
             Config::load($extra['assets-config-class']);
         }
         if (!empty($extra)) {
             Config::overload($extra);
         }
         $assets_db_filename = isset($extra['assets-db-filename']) ? $extra['assets-db-filename'] : Config::get('assets-db-filename');
         $db_file = $this->getRootDirectory() . '/' . $this->getVendorDirectory() . '/' . $assets_db_filename;
         if (file_exists($db_file)) {
             $json_assets = json_decode(file_get_contents($db_file), true);
             $this->setAssetsDirectory(!is_null($assets_dir) ? $assets_dir : (isset($json_assets['assets_dir']) ? $json_assets['assets_dir'] : Config::get('assets-dir')))->setAssetsVendorDirectory(isset($json_assets['assets_vendor_dir']) ? $json_assets['assets_vendor_dir'] : Config::get('assets-vendor-dir'))->setDocumentRoot(!is_null($document_root) ? $document_root : (isset($json_assets['document_root']) ? $json_assets['document_root'] : Config::get('document-root')))->setAssetsDb(!empty($json_assets['packages']) ? $json_assets['packages'] : array());
         } else {
             throw new \Exception(sprintf('Assets json DB "%s" not found!', $db_file));
         }
         $this->validatePresets();
     } catch (\Exception $e) {
         throw $e;
     }
 }