diff --git a/.gitignore b/.gitignore index 4ab0e73..48b8bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1 @@ -.* -._* -.DS_Store -.svn -!.gitignore -Desktop.ini -Thumbs.db - -.fuelphp-phpstorm -.idea -nbproject - -fuel/app/logs/*/*/* -fuel/app/cache/*/* -/vendor/ \ No newline at end of file +vendor/ diff --git a/bootstrap.php b/bootstrap.php deleted file mode 100644 index dd22626..0000000 --- a/bootstrap.php +++ /dev/null @@ -1,15 +0,0 @@ - __DIR__.'/classes/Foolz/Plugin/Void.php', - 'Foolz\\Plugin\\Util' => __DIR__.'/classes/Foolz/Plugin/Util.php', - 'Foolz\\Plugin\\Loader' => __DIR__.'/classes/Foolz/Plugin/Loader.php', - 'Foolz\\Plugin\\Plugin' => __DIR__.'/classes/Foolz/Plugin/Plugin.php', - 'Foolz\\Plugin\\Hook' => __DIR__.'/classes/Foolz/Plugin/Hook.php', - 'Foolz\\Plugin\\Event' => __DIR__.'/classes/Foolz/Plugin/Event.php', - 'Foolz\\Plugin\\Result' => __DIR__.'/classes/Foolz/Plugin/Result.php', -)); \ No newline at end of file diff --git a/classes/Foolz/Package/AssetManager.php b/classes/Foolz/Package/AssetManager.php index 93812e3..440d937 100644 --- a/classes/Foolz/Package/AssetManager.php +++ b/classes/Foolz/Package/AssetManager.php @@ -4,133 +4,124 @@ class AssetManager { - /** - * The package creating this object - * - * @var \Foolz\Package\Package|null - */ - protected $package = null; - - /** - * The directory where the files should be put so they are reachable via an URL - * - * @var string - */ - protected $public_dir = ""; - - /** - * The base URL where the package files can be found at - * - * @var string - */ - protected $base_url = ""; - - /** - * Check if the package extends another, avoids some file checks - * - * @var bool - */ - protected $does_extend = true; - - /** - * Create a new instance of the asset manager - * - * @param \Foolz\Package\Package $package The reference to the package creating this asset manager - * - * @return \Foolz\Package\AssetManager - */ - public function __construct(\Foolz\Package\Package $package) - { - $this->package = $package; - $this->public_dir = $this->getPackage()->getLoader()->getPublicDir(); - $this->base_url = $this->getPackage()->getLoader()->getBaseUrl(); - - // load the assets - if ( ! file_exists($this->getPublicDir())) - { - $this->clearAssets(); - $this->loadAssets(); - } - - try - { - $this->getPackage()->getExtended(); - $this->does_extend = true; - } - catch (\OutOfBoundsException $e) - { - $this->does_extend = false; - } - - } - - /** - * Returns the Package object that created this instance of AssetManager - * - * @return \Foolz\Package\Package|null The Package object that created this instance of AssetManager - */ - public function getPackage() - { - return $this->package; - } - - /** - * Returns the path to the directory where the public files get loaded - * - * @return string The path - */ - protected function getPublicDir() - { - return $this->public_dir.$this->getPackage()->getConfig('name') - .'/assets-'.$this->getPackage()->getConfig('version').'/'; - } - - /** - * Returns an URL to the asset being requested - * - * @param $path $path The relative path to the asset to link to - * - * @return string The full URL to the asset - */ - public function getAssetLink($path) - { - $candidate = $this->base_url.$this->getPackage()->getConfig('name') - .'/assets-'.$this->getPackage()->getConfig('version').'/'.$path; - - if ( ! $this->does_extend || file_exists($this->getPublicDir().$path)) - { - return $candidate; - } - - return $this->getPackage()->getExtended()->getAssetManager()->getAssetLink($path); - } - - /** - * Loads all the asset files from the package folder - */ - protected function loadAssets() - { - if ( ! file_exists($this->getPublicDir())) - { - mkdir($this->getPublicDir(), 0777, true); - } - - Util::copy($this->getPackage()->getDir().'assets', rtrim($this->getPublicDir(), '/')); - } - - /** - * Clears all the files in the public package directory - * - * @return \Foolz\Package\AssetManager The current object - */ - public function clearAssets() - { - // get it just right out of the assets folder - if (file_exists($this->public_dir.$this->getPackage()->getConfig('name'))) - { - Util::delete($this->public_dir.$this->getPackage()->getConfig('name')); - } - - return $this; - } + /** + * The package creating this object + * + * @var \Foolz\Package\Package|null + */ + protected $package = null; + + /** + * The directory where the files should be put so they are reachable via an URL + * + * @var string + */ + protected $public_dir = ''; + + /** + * The base URL where the package files can be found at + * + * @var string + */ + protected $base_url = ''; + + /** + * Check if the package extends another, avoids some file checks + * + * @var bool + */ + protected $does_extend = true; + + /** + * Create a new instance of the asset manager + * + * @param \Foolz\Package\Package $package The reference to the package creating this asset manager + * + * @return \Foolz\Package\AssetManager + */ + public function __construct(\Foolz\Package\Package $package) + { + $this->package = $package; + $this->public_dir = $this->getPackage()->getLoader()->getPublicDir(); + $this->base_url = $this->getPackage()->getLoader()->getBaseUrl(); + + // load the assets + if (!file_exists($this->getPublicDir())) { + $this->clearAssets(); + $this->loadAssets(); + } + + try { + $this->getPackage()->getExtended(); + $this->does_extend = true; + } catch (\OutOfBoundsException $e) { + $this->does_extend = false; + } + + } + + /** + * Returns the Package object that created this instance of AssetManager + * + * @return \Foolz\Package\Package|null The Package object that created this instance of AssetManager + */ + public function getPackage() + { + return $this->package; + } + + /** + * Returns the path to the directory where the public files get loaded + * + * @return string The path + */ + protected function getPublicDir() + { + return $this->public_dir.$this->getPackage()->getConfig('name') + .'/assets-'.$this->getPackage()->getConfig('version').'/'; + } + + /** + * Returns an URL to the asset being requested + * + * @param $path $path The relative path to the asset to link to + * + * @return string The full URL to the asset + */ + public function getAssetLink($path) + { + if (!$this->does_extend || file_exists($this->getPublicDir().$path)) { + return $this->base_url.$this->getPackage()->getConfig('name') + .'/assets-'.$this->getPackage()->getConfig('version').'/'.$path; + } + + return $this->getPackage()->getExtended()->getAssetManager()->getAssetLink($path); + } + + /** + * Loads all the asset files from the package folder + */ + protected function loadAssets() + { + if (!file_exists($this->getPublicDir())) { + mkdir($this->getPublicDir(), 0777, true); + } + + Util::copy($this->getPackage()->getDir().'assets', rtrim($this->getPublicDir(), '/')); + } + + /** + * Clears all the files in the public package directory + * + * @return \Foolz\Package\AssetManager The current object + */ + public function clearAssets() + { + // get it just right out of the assets folder + if (file_exists($this->public_dir.$this->getPackage()->getConfig('name'))) { + Util::delete($this->public_dir.$this->getPackage()->getConfig('name')); + } + + return $this; + } } diff --git a/classes/Foolz/Package/Loader.php b/classes/Foolz/Package/Loader.php index 6da756c..8a7543a 100644 --- a/classes/Foolz/Package/Loader.php +++ b/classes/Foolz/Package/Loader.php @@ -11,264 +11,251 @@ */ class Loader { - /** - * The type of package in use. Can be in example 'theme' or 'plugin' - * Override this to change type of package - * - * @var string - */ - protected $type_name = 'package'; - - /** - * The class into which the resulting objects are created. - * Override this, in example Foolz\Plugin\Plugin or Foolz\Theme\Theme - * - * @var string - */ - protected $type_class = 'Foolz\Package\Package'; - - /** - * The instances of the Loader class - * - * @var \Foolz\Package\Loader[] - */ - protected static $instances = []; - - /** - * The dirs in which to look for packages - * - * @var null|array - */ - protected $dirs = null; - - /** - * The packages found - * - * @var null|array as first key the dir name, as second key the slug - */ - protected $packages = null; - - /** - * Tells if packages should be reloaded - * - * @var boolean - */ - protected $reload = true; - - /** - * The Composer autoloader - * - * @var \Composer\Autoload\ClassLoader - */ - protected $composer_loader = null; - - /** - * Creates or returns a named instance of Loader - * - * @param string $instance The name of the instance to use or create - * - * @return \Foolz\Package\Loader - */ - public static function forge($instance = 'default') - { - if ( ! isset(static::$instances[$instance])) - { - return static::$instances[$instance] = new static(); - } - - return static::$instances[$instance]; - } - - /** - * Destroys a named instance and unregisters its autoloader - * - * @param string $instance The name of the instance to use or create - */ - public static function destroy($instance = 'default') - { - unset(static::$instances[$instance]); - } - - /** - * Adds a directory to the array of directories to search packages in - * - * @param string $dir_name If $dir is not set this sets both the name and the dir equal - * @param null|string $dir The dir where to look for packages - * - * @return \Foolz\Package\Loader The current object - * @throws \DomainException If the directory is not found - */ - public function addDir($dir = null) - { - if ( ! is_dir($dir)) - { - throw new \DomainException('Directory not found.'); - } - - $this->dirs[] = rtrim($dir, '/'); - - // set the flag to reload packages on demand - $this->reload = true; - - return $this; - } - - /** - * Looks for packages in the specified directories and creates the objects - */ - public function find() - { - if ($this->packages === null) - { - $this->packages = array(); - } - - foreach ($this->dirs as $dir) - { - $vendor_paths = $this->findDirs($dir); - - foreach ($vendor_paths as $vendor_name => $vendor_path) - { - $package_paths = $this->findDirs($vendor_path); - - foreach ($package_paths as $package_name => $package_path) - { - if ( ! isset($this->packages[$vendor_name.'/'.$package_name])) - { - /* @var $package \Foolz\Package\Package */ - $package = new $this->type_class($package_path); - $package->setLoader($this); - $this->packages[$vendor_name.'/'.$package_name] = $package; - } - } - } - } - } - - /** - * Internal function to find all directories at the path - * - * @param string $path The path to look into - * - * @return array The paths with as they the last part of the path - */ - protected function findDirs($path) - { - $result = array(); - $fp = opendir($path); - - while (false !== ($file = readdir($fp))) - { - // Remove '.', '..' - if (in_array($file, array('.', '..'))) - { - continue; - } - - if (is_dir($path.'/'.$file)) - { - $result[$file] = $path.'/'.$file; - } - } - - closedir($fp); - - return $result; - } - - /** - * Gets all the packages or the packages from the directory - * - * @return \Foolz\Package\Package[] All the packages or the packages in the directory - * @throws \OutOfBoundsException If there isn't such a $dir_name set - */ - public function getAll() - { - if ($this->reload === true) - { - $this->find(); - } - - return $this->packages; - } - - /** - * Gets a single package object - * - * @param string $slug The slug of the package - * - * @return \Foolz\Package\Package - * @throws \OutOfBoundsException if the package doesn't exist - */ - public function get($slug) - { - $packages = $this->getAll(); - - if ( ! isset($packages[$slug])) - { - throw new \OutOfBoundsException('There is no such a package.'); - } - - return $packages[$slug]; - } - - /** - * Set the public directory where files can be copied into - * - * @param $public_dir The path - * - * @return \Foolz\Theme\Loader - */ - public function setPublicDir($public_dir) - { - $this->public_dir = rtrim($public_dir, '/').'/'; - - return $this; - } - - /** - * Returns the public directory where files are copied into - * - * @return string The path - * @throws \BadMethodCallException If the public dir wasn't set - */ - public function getPublicDir() - { - if ($this->public_dir === null) - { - throw new \BadMethodCallException('The public dir wasn\'t set'); - } - - return $this->public_dir; - } - - /** - * Set the base URL that points to the public directory - * - * @param $base_url The URL - * - * @return \Foolz\Theme\Loader - */ - public function setBaseUrl($base_url) - { - $this->base_url = rtrim($base_url, '/').'/'; - - return $this; - } - - /** - * Returns the base URL that points to the public directory - * - * @return string The URL - * @throws \BadMethodCallException If the base url wasn't set - */ - public function getBaseUrl() - { - if ($this->base_url === null) - { - throw new \BadMethodCallException('The base url wasn\'t set'); - } - - return $this->base_url; - } + /** + * The type of package in use. Can be in example 'theme' or 'plugin' + * Override this to change type of package + * + * @var string + */ + protected $type_name = 'package'; + + /** + * The class into which the resulting objects are created. + * Override this, in example Foolz\Plugin\Plugin or Foolz\Theme\Theme + * + * @var string + */ + protected $type_class = 'Foolz\Package\Package'; + + /** + * The instances of the Loader class + * + * @var \Foolz\Package\Loader[] + */ + protected static $instances = []; + + /** + * The dirs in which to look for packages + * + * @var null|array + */ + protected $dirs = null; + + /** + * The packages found + * + * @var null|array as first key the dir name, as second key the slug + */ + protected $packages = null; + + /** + * Tells if packages should be reloaded + * + * @var boolean + */ + protected $reload = true; + + /** + * The Composer autoloader + * + * @var \Composer\Autoload\ClassLoader + */ + protected $composer_loader = null; + + /** + * Creates or returns a named instance of Loader + * + * @param string $instance The name of the instance to use or create + * + * @return \Foolz\Package\Loader + */ + public static function forge($instance = 'default') + { + if (!isset(static::$instances[$instance])) { + return static::$instances[$instance] = new static(); + } + + return static::$instances[$instance]; + } + + /** + * Destroys a named instance and unregisters its autoloader + * + * @param string $instance The name of the instance to use or create + */ + public static function destroy($instance = 'default') + { + unset(static::$instances[$instance]); + } + + /** + * Adds a directory to the array of directories to search packages in + * + * @param string $dir_name If $dir is not set this sets both the name and the dir equal + * @param null|string $dir The dir where to look for packages + * + * @return \Foolz\Package\Loader The current object + * @throws \DomainException If the directory is not found + */ + public function addDir($dir = null) + { + if (!is_dir($dir)) { + throw new \DomainException('Directory not found.'); + } + + $this->dirs[] = rtrim($dir, '/'); + + // set the flag to reload packages on demand + $this->reload = true; + + return $this; + } + + /** + * Looks for packages in the specified directories and creates the objects + */ + public function find() + { + if ($this->packages === null) { + $this->packages = array(); + } + + foreach ($this->dirs as $dir) { + $vendor_paths = $this->findDirs($dir); + + foreach ($vendor_paths as $vendor_name => $vendor_path) { + $package_paths = $this->findDirs($vendor_path); + + foreach ($package_paths as $package_name => $package_path) { + if (!isset($this->packages[$vendor_name.'/'.$package_name])) { + /* @var $package \Foolz\Package\Package */ + $package = new $this->type_class($package_path); + $package->setLoader($this); + + $this->packages[$vendor_name.'/'.$package_name] = $package; + } + } + } + } + } + + /** + * Internal function to find all directories at the path + * + * @param string $path The path to look into + * + * @return array The paths with as they the last part of the path + */ + protected function findDirs($path) + { + $result = array(); + $fp = opendir($path); + + while (false !== ($file = readdir($fp))) { + // Remove '.', '..' + if (in_array($file, array('.', '..'))) { + continue; + } + + if (is_dir($path.'/'.$file)) { + $result[$file] = $path.'/'.$file; + } + } + + closedir($fp); + + return $result; + } + + /** + * Gets all the packages or the packages from the directory + * + * @return \Foolz\Package\Package[] All the packages or the packages in the directory + * @throws \OutOfBoundsException If there isn't such a $dir_name set + */ + public function getAll() + { + if ($this->reload === true) { + $this->find(); + } + + return $this->packages; + } + + /** + * Gets a single package object + * + * @param string $slug The slug of the package + * + * @return \Foolz\Package\Package + * @throws \OutOfBoundsException if the package doesn't exist + */ + public function get($slug) + { + $packages = $this->getAll(); + + if (!isset($packages[$slug])) { + throw new \OutOfBoundsException('There is no such a package.'); + } + + return $packages[$slug]; + } + + /** + * Set the public directory where files can be copied into + * + * @param $public_dir The path + * + * @return \Foolz\Theme\Loader + */ + public function setPublicDir($public_dir) + { + $this->public_dir = rtrim($public_dir, '/').'/'; + + return $this; + } + + /** + * Returns the public directory where files are copied into + * + * @return string The path + * @throws \BadMethodCallException If the public dir wasn't set + */ + public function getPublicDir() + { + if ($this->public_dir === null) { + throw new \BadMethodCallException('The public dir was not set.'); + } + + return $this->public_dir; + } + + /** + * Set the base URL that points to the public directory + * + * @param $base_url The URL + * + * @return \Foolz\Theme\Loader + */ + public function setBaseUrl($base_url) + { + $this->base_url = rtrim($base_url, '/').'/'; + + return $this; + } + + /** + * Returns the base URL that points to the public directory + * + * @return string The URL + * @throws \BadMethodCallException If the base url wasn't set + */ + public function getBaseUrl() + { + if ($this->base_url === null) { + throw new \BadMethodCallException('The base url was not set.'); + } + + return $this->base_url; + } } diff --git a/classes/Foolz/Package/Package.php b/classes/Foolz/Package/Package.php index bcfd509..e92fcfa 100644 --- a/classes/Foolz/Package/Package.php +++ b/classes/Foolz/Package/Package.php @@ -11,329 +11,312 @@ */ class Package { - /** - * The Loader object that created this object - * - * @var \Foolz\Package\Loader - */ - protected $loader; - - /** - * The name given to the dir to recall it from the loader - * - * @var type - */ - protected $dir_name; - - /** - * The path to this package - * - * @var string - */ - protected $dir; - - /** - * Loaded JSON config - * - * @var null|array - */ - protected $json_config = null; - - /** - * Loaded PHP config - * - * @var null|array - */ - protected $config = null; - - /** - * Remember if the plugin has been bootstrapped already - * - * @var boolean - */ - protected $bootstrapped = false; - - /** - * Stores the asset manager for the package - * - * @var AssetManager|null - */ - protected $asset_manager = null; - - /** - * Sets the directory of the package - * - * @param string $dir The path to the package - * - * @throws \DomainException When the path is not found - */ - public function __construct($dir) - { - $dir = rtrim($dir,'/').'/'; - if ( ! file_exists($dir.'composer.json')) - { - throw new \DomainException('Directory not found.'); - } - - $this->dir = $dir; - } - - /** - * Sets a loader to use the relative - * - * @param \Foolz\Package\Loader $loader - * - * @return $this - */ - public function setLoader(\Foolz\Package\Loader $loader) - { - $this->loader = $loader; - return $this; - } - - /** - * Gets the loader that created this object - * - * @return \Foolz\Package\Loader - */ - public function getLoader() - { - return $this->loader; - } - - /** - * Gets the path to the package - * - * @return string - */ - public function getDir() - { - return $this->dir; - } - - /** - * Plugs the package in the Composer autoloader - */ - public function enableAutoloader() - { - $psr = $this->getConfig('autoload.psr-0', false); - - if ( ! $psr) - { - return; - } - - foreach ($psr as $key => $item) - { - $loader = new \Composer\Autoload\ClassLoader(); - $loader->add($key, $this->getDir().$item); - $loader->register(); - } - } - - /** - * Gets the content of the composer.json - * - * @param string $section keys of the array separated by dots - * @param mixed $fallback - * @return mixed - * @throws \DomainException if there is no such config item and there was no fallback set - * - * @deprecated 23/03/2013 Trying to use JSON straight away - */ - public function getJsonConfig($section = null, $fallback = null) - { - if ($this->json_config === null) - { - $file = $this->getDir().'composer.json'; - - // should never happen as we check for composer.json on instantiation - if ( ! file_exists($file)) - { - // @codeCoverageIgnoreStart - throw new \DomainException; - // @codeCoverageIgnoreEnd - } - - $this->json_config = json_decode(file_get_contents($file), true); - - if ($this->json_config === null) - { - throw new \DomainException; - } - } - - if ($section === null) - { - return $this->json_config; - } - - // if there wasn't an actual fallback set - if (func_num_args() !== 2) - { - return Util::dottedConfig($this->json_config, $section, new Void); - } - - return Util::dottedConfig($this->json_config, $section, $fallback); - } - - /** - * Converts the JSON to a PHP config to improve speed - * - * @return \Foolz\Package\Package - * - * @deprecated 23/03/2013 Trying to use JSON straight away - */ - public function jsonToConfig() - { - $config = $this->getJsonConfig(); - - Util::saveArrayToFile($this->getDir().'composer.php', $config); - return $this; - } - - /** - * Gets the content of the config - * - * @param string $section keys of the array separated by dots - * @param mixed $fallback - * @return mixed - * @throws \DomainException if there is no such config item and there was no fallback set - */ - public function getConfig($section = null, $fallback = null) - { - - if ($this->config === null) - { - /* - * Code to use PHP arrays as cache - * - $php_file = $this->getDir().'composer.php'; - - if (file_exists($php_file) === false) - { - $this->jsonToConfig(); - } - - $this->config = include $php_file; - */ - - $this->config = json_decode(file_get_contents($this->getDir().'composer.json'), true); - } - - if ($section === null) - { - return $this->config; - } - - // if there wasn't an actual fallback set - if (func_num_args() !== 2) - { - return Util::dottedConfig($this->config, $section, new Void); - } - - return Util::dottedConfig($this->config, $section, $fallback); - } - - /** - * Destroys the composer.php to recreate it from the composer.json - * - * @return \Foolz\Package\Package - */ - public function refreshConfig() - { - if (file_exists($this->getDir().'composer.php')) - { - unlink($this->getDir().'composer.php'); - } - - $this->clearJsonConfig(); - $this->clearConfig(); - return $this; - } - - /** - * Clears the json_config variable to reload from JSON - * - * @return \Foolz\Package\Package - * - * @deprecated 23/03/2013 Trying to use JSON straight away - */ - public function clearJsonConfig() - { - $this->json_config = null; - return $this; - } - - /** - * Clears the config variable to reload from composer.php - * - * @return \Foolz\Package\Package - */ - public function clearConfig() - { - $this->config = null; - return $this; - } - - /** - * Runs the bootstrap file - * - * @return \Foolz\Package\Package - */ - public function bootstrap() - { - include $this->getDir().'bootstrap.php'; - $this->bootstrapped = true; - return $this; - } - - /** - * Tells if the bootstrap file has been run at least once - * - * @return boolean True if the bootstrap file has been run, false otherwise - */ - public function isBootstrapped() - { - return $this->bootstrapped; - } - - /** - * Returns an AssetManager object to deal with the assets - * - * @return \Foolz\Package\AssetManager A new instance of the AssetManager - */ - public function getAssetManager() - { - if ($this->asset_manager !== null) - { - return $this->asset_manager; - } - return $this->asset_manager = new AssetManager($this); - } - - /** - * Checks for the existence of a theme to extend and returns the theme - * - * @return \Foolz\Package\Package The base theme we're extending with the current - * @throws \OutOfBoundsException If the theme is not found or no extended theme has been specified - */ - public function getExtended() - { - $extended = $this->getConfig('extra.extends', null); - - if ($extended === null) - { - throw new \OutOfBoundsException('No package to extend.'); - } - - try - { - return $this->getLoader()->get($extended); - } - catch (\OutOfBoundsException $e) - { - throw new \OutOfBoundsException('No such package available for extension.'); - } - } + /** + * The Loader object that created this object + * + * @var \Foolz\Package\Loader + */ + protected $loader; + + /** + * The name given to the dir to recall it from the loader + * + * @var type + */ + protected $dir_name; + + /** + * The path to this package + * + * @var string + */ + protected $dir; + + /** + * Loaded JSON config + * + * @var null|array + */ + protected $json_config = null; + + /** + * Loaded PHP config + * + * @var null|array + */ + protected $config = null; + + /** + * Remember if the plugin has been bootstrapped already + * + * @var boolean + */ + protected $bootstrapped = false; + + /** + * Stores the asset manager for the package + * + * @var AssetManager|null + */ + protected $asset_manager = null; + + /** + * Sets the directory of the package + * + * @param string $dir The path to the package + * + * @throws \DomainException When the path is not found + */ + public function __construct($dir) + { + $dir = rtrim($dir,'/').'/'; + if (!file_exists($dir.'composer.json')) { + throw new \DomainException('Directory not found.'); + } + + $this->dir = $dir; + } + + /** + * Sets a loader to use the relative + * + * @param \Foolz\Package\Loader $loader + * + * @return $this + */ + public function setLoader(\Foolz\Package\Loader $loader) + { + $this->loader = $loader; + + return $this; + } + + /** + * Gets the loader that created this object + * + * @return \Foolz\Package\Loader + */ + public function getLoader() + { + return $this->loader; + } + + /** + * Gets the path to the package + * + * @return string + */ + public function getDir() + { + return $this->dir; + } + + /** + * Plugs the package in the Composer autoloader + */ + public function enableAutoloader() + { + $psr0 = $this->getConfig('autoload.psr-0', []); + $psr4 = $this->getConfig('autoload.psr-4', []); + + if (!empty($psr0) && !empty($psr4)) { + $loader = new \Composer\Autload\ClassLoader(); + + // load psr-0 + foreach ($psr0 as $namespace => $path) { + $loader->add($namespace, $this->getDir().$path); + } + + // load psr-4 + foreach ($psr4 as $namespace => $path) { + $loader->addPsr4($namespace, $this->getDir().$path); + } + + $loader->register(); + } + } + + /** + * Gets the content of the composer.json + * + * @param string $section keys of the array separated by dots + * @param mixed $fallback + * @return mixed + * @throws \DomainException if there is no such config item and there was no fallback set + * + * @deprecated 23/03/2013 Trying to use JSON straight away + */ + public function getJsonConfig($section = null, $fallback = null) + { + if ($this->json_config === null) { + $file = $this->getDir().'composer.json'; + + // should never happen as we check for composer.json on instantiation + if (!file_exists($file)) { + // @codeCoverageIgnoreStart + throw new \DomainException; + // @codeCoverageIgnoreEnd + } + + $this->json_config = json_decode(file_get_contents($file), true); + + if ($this->json_config === null) { + throw new \DomainException; + } + } + + if ($section === null) { + return $this->json_config; + } + + // if there wasn't an actual fallback set + if (func_num_args() !== 2) { + return Util::dottedConfig($this->json_config, $section, new Void); + } + + return Util::dottedConfig($this->json_config, $section, $fallback); + } + + /** + * Converts the JSON to a PHP config to improve speed + * + * @return \Foolz\Package\Package + * + * @deprecated 23/03/2013 Trying to use JSON straight away + */ + public function jsonToConfig() + { + $config = $this->getJsonConfig(); + + Util::saveArrayToFile($this->getDir().'composer.php', $config); + + return $this; + } + + /** + * Gets the content of the config + * + * @param string $section keys of the array separated by dots + * @param mixed $fallback + * @return mixed + * @throws \DomainException if there is no such config item and there was no fallback set + */ + public function getConfig($section = null, $fallback = null) + { + + if ($this->config === null) { + $this->config = json_decode(file_get_contents($this->getDir().'composer.json'), true); + } + + if ($section === null) { + return $this->config; + } + + // if there wasn't an actual fallback set + if (func_num_args() !== 2) { + return Util::dottedConfig($this->config, $section, new Void); + } + + return Util::dottedConfig($this->config, $section, $fallback); + } + + /** + * Destroys the composer.php to recreate it from the composer.json + * + * @return \Foolz\Package\Package + */ + public function refreshConfig() + { + if (file_exists($this->getDir().'composer.php')) { + unlink($this->getDir().'composer.php'); + } + + $this->clearJsonConfig(); + $this->clearConfig(); + return $this; + } + + /** + * Clears the json_config variable to reload from JSON + * + * @return \Foolz\Package\Package + * + * @deprecated 23/03/2013 Trying to use JSON straight away + */ + public function clearJsonConfig() + { + $this->json_config = null; + + return $this; + } + + /** + * Clears the config variable to reload from composer.php + * + * @return \Foolz\Package\Package + */ + public function clearConfig() + { + $this->config = null; + + return $this; + } + + /** + * Runs the bootstrap file + * + * @return \Foolz\Package\Package + */ + public function bootstrap() + { + include $this->getDir().'bootstrap.php'; + $this->bootstrapped = true; + + return $this; + } + + /** + * Tells if the bootstrap file has been run at least once + * + * @return boolean True if the bootstrap file has been run, false otherwise + */ + public function isBootstrapped() + { + return $this->bootstrapped; + } + + /** + * Returns an AssetManager object to deal with the assets + * + * @return \Foolz\Package\AssetManager A new instance of the AssetManager + */ + public function getAssetManager() + { + if ($this->asset_manager !== null) { + return $this->asset_manager; + } + + return $this->asset_manager = new AssetManager($this); + } + + /** + * Checks for the existence of a theme to extend and returns the theme + * + * @return \Foolz\Package\Package The base theme we're extending with the current + * @throws \OutOfBoundsException If the theme is not found or no extended theme has been specified + */ + public function getExtended() + { + $extended = $this->getConfig('extra.extends', null); + + if ($extended === null) { + throw new \OutOfBoundsException('No package to extend.'); + } + + try { + return $this->getLoader()->get($extended); + } catch (\OutOfBoundsException $e) { + throw new \OutOfBoundsException('No such package available for extension.'); + } + } } diff --git a/classes/Foolz/Package/Util.php b/classes/Foolz/Package/Util.php index ea49801..3b2a975 100644 --- a/classes/Foolz/Package/Util.php +++ b/classes/Foolz/Package/Util.php @@ -1,6 +1,7 @@ isDir()) { - rmdir($file->getPathname()); - } else { - unlink($file->getPathname()); - } - } + foreach ($iterator as $file) { + if ($file->isDir()) { + rmdir($file->getPathname()); + } else { + unlink($file->getPathname()); + } + } - rmdir($path); - } else { - unlink($path); - } - } + rmdir($path); + } else { + unlink($path); + } + } - /** - * Copy a file or recursively copy a directories contents - * - * From: http://davidhancock.co/2012/11/useful-php-functions-for-dealing-with-the-file-system/ - * - * @param string $src The path to the source file/directory - * @param string $dst The path to the destination directory - * @return void - */ - public static function copy($src, $dst) - { - if (is_dir($src)) { - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($src, RecursiveDirectoryIterator::SKIP_DOTS), - RecursiveIteratorIterator::SELF_FIRST - ); + /** + * Copy a file or recursively copy a directories contents + * + * From: http://davidhancock.co/2012/11/useful-php-functions-for-dealing-with-the-file-system/ + * + * @param string $src The path to the source file/directory + * @param string $dst The path to the destination directory + * @return void + */ + public static function copy($src, $dst) + { + if (is_dir($src)) { + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($src, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::SELF_FIRST + ); - foreach ($iterator as $file) { - if ($file->isDir()) { - mkdir($dst.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); - } else { - copy($file, $dst.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); - } - } - } else { - copy($src, $dst); - } - } + foreach ($iterator as $file) { + if ($file->isDir()) { + mkdir($dst.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); + } else { + copy($file, $dst.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); + } + } + } else { + copy($src, $dst); + } + } } diff --git a/classes/Foolz/Package/Void.php b/classes/Foolz/Package/Void.php index de2ae5d..7d08f1e 100644 --- a/classes/Foolz/Package/Void.php +++ b/classes/Foolz/Package/Void.php @@ -12,14 +12,13 @@ class Void { - /** - * Use to set default parameters to void - * - * @return \Foolz\Plugin\Void - */ - public static function forge() - { - return new static(); - } - -} \ No newline at end of file + /** + * Use to set default parameters to void + * + * @return \Foolz\Plugin\Void + */ + public static function forge() + { + return new static(); + } +} diff --git a/composer.json b/composer.json index 5480b6c..82c1dd7 100644 --- a/composer.json +++ b/composer.json @@ -18,4 +18,4 @@ "Foolz\\Package": "classes/" } } -} \ No newline at end of file +} diff --git a/composer.phar b/composer.phar deleted file mode 100644 index 8090200..0000000 Binary files a/composer.phar and /dev/null differ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 36ec40b..327146f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,6 +2,6 @@ // autoload.php generated by Composer -require_once __DIR__ . '/../vendor/composer' . '/autoload_real.php'; +require_once __DIR__.'/../vendor/composer/autoload_real.php'; return ComposerAutoloaderInit::getLoader(); diff --git a/tests/mock/foolz/fake/classes/Foolz/Fake/Fake.php b/tests/mock/foolz/fake/classes/Foolz/Fake/Fake.php index 3c5f349..2fb62a3 100644 --- a/tests/mock/foolz/fake/classes/Foolz/Fake/Fake.php +++ b/tests/mock/foolz/fake/classes/Foolz/Fake/Fake.php @@ -4,8 +4,8 @@ class Fake { - public static function fake() - { - return 'I am fake.'; - } -} \ No newline at end of file + public static function fake() + { + return 'I am fake.'; + } +} diff --git a/tests/mock/foolz/fake/composer.json b/tests/mock/foolz/fake/composer.json index ae02b07..561e001 100644 --- a/tests/mock/foolz/fake/composer.json +++ b/tests/mock/foolz/fake/composer.json @@ -1,27 +1,24 @@ { - "name": "foolz/fake", - "type": "foolz-plugin", - "description": "A fake plugin", - "keywords": ["fake", "mock"], - "homepage": "http://www.foolz.us", - "license": "Apache-2.0", - "authors": [{"name": "foolz", "email": "support@foolz.us"}], - "support": { - "email": "support@foolz.us", - "irc": "irc://irc.irchighway.net/fooldriver" - }, - - "require" : { - "foolz/plugin": "dev" - }, - - "autoload": { - "psr-0": { - "Foolz\\Fake" : "classes/" - } - }, - - "extra": { - "name": "Fake" - } -} \ No newline at end of file + "name": "foolz/fake", + "type": "foolz-plugin", + "description": "A fake plugin", + "keywords": ["fake", "mock"], + "homepage": "http://www.foolz.us", + "license": "Apache-2.0", + "authors": [{"name": "foolz", "email": "support@foolz.us"}], + "support": { + "email": "support@foolz.us", + "irc": "irc://irc.irchighway.net/fooldriver" + }, + "require" : { + "foolz/plugin": "dev" + }, + "autoload": { + "psr-0": { + "Foolz\\Fake" : "classes/" + } + }, + "extra": { + "name": "Fake" + } +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 2bde51f..f3d4651 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,9 +1,9 @@ - - - classes - - + + + classes + +