Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/php/Phix_Project/Phix/ClassConflictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright (c) 2010 Martin Wernståhl.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Martin Wernståhl nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Phix_Project
* @subpackage Phix
* @author Martin Wernståhl <m4rw3r@gmail.com>
* @copyright 2010 Martin Wernståhl
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://github.com/m4rw3r
* @version @@PACKAGE_VERSION@@
*/

namespace Phix_Project\Phix;

/**
* Exception telling that a conflicting class name was discovered
* by the class finder.
*/
class ClassConflictException extends \RuntimeException
{
// TODO: Is this threshold good?
const MAX_MESSAGE_LENGTH = 40;

protected $classes = array();

protected $files = array();

/**
* @param array(array('class' => string, 'file' => string), ...)
*/
function __construct(array $conflicts)
{
$this->classes = array_map(function($elem)
{
return $elem['class'];
}, $conflicts);

$this->files = array_map(function($elem)
{
return $elem['file'];
}, $conflicts);

$classlist = implode(', ', $this->classes);
$filelist = implode(', ', $this->files);

if(strlen($classlist) > static::MAX_MESSAGE_LENGTH)
{
$classlist = substr($classlist, 0, static::MAX_MESSAGE_LENGTH).'...';
}

if(strlen($filelist) > static::MAX_MESSAGE_LENGTH)
{
$filelist = substr($filelist, 0, static::MAX_MESSAGE_LENGTH).'...';
}

parent::__construct('ClassFinder: Found conflicting class(es): '.$classlist.' in files: '.$filelist);
}

// ------------------------------------------------------------------------

/**
* Returns a list of unique class names which were conflicted.
*
* @return array(string)
*/
public function getConflictingClasses()
{
return array_unique($this->classes);
}

// ------------------------------------------------------------------------

/**
* Returns a list of files containing conflicting classes.
*
* @return array(string)
*/
public function getConflictingFiles()
{
return array_unique($this->files);
}
}
39 changes: 34 additions & 5 deletions src/php/Phix_Project/Phix/ClassFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,27 @@
* Searches through a set of paths for classes, returns a list of classes
* and in which file they are located, does NOT include the files.
*
* If duplicate classes are found, returns only the first file that
* contains the class
* Throws a PHP exception if duplicate files are found, but the classes
* can still be found by calling getClassFiles() again but won't contain
* the duplicates (only the first occurrence of the class).
*
* Example usage:
* <code>
* $finder = new \Phix_Project\Phix\ClassFinder(array('.', '../src'));
*
* try
* {
* $classes = $finder->getClassFiles();
* }
* catch(\Phix_Project\Phix\ClassConflictException $e)
* {
* // Take note of the error:
* echo "Conflicting classes!";
*
* // Let the operation continue, but might contain the wrong class-file:
* $classes = $finder->getClassFiles();
* }
* </code>
*/

class ClassFinder
Expand Down Expand Up @@ -108,6 +127,8 @@ public function getClassFiles()
return $this->list;
}

$conflicts = array();

foreach($this->paths as $path)
{
// Search the folder
Expand All @@ -119,15 +140,23 @@ public function getClassFiles()
{
foreach($this->getClasses($name) as $class)
{
if(!isset($this->list[$class]))
if(isset($this->list[$class]))
{
$conflicts[] = array('class' => $class, 'file' => $name);
}
else
{
// class is new!
$this->list[$class] = $name;
$this->list[$class] = $name;
}
}
}
}

if( ! empty($conflicts))
{
throw new ClassConflictException($conflicts);
}

return $this->list;
}

Expand Down
19 changes: 17 additions & 2 deletions src/php/Phix_Project/Phix/ExtensionsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@

class ExtensionsFinder
{
protected $context;

protected $foldersToSearch = array();

public function __construct()
public function __construct(Context $context)
{
$this->context = $context;
$this->addPhpSearchPathToSearchList();
}

Expand All @@ -72,8 +75,20 @@ public function findExtensions()

// Find all classes in php files whose paths contain "PhixCommands":
$classFinder = new ClassFinder(explode(\PATH_SEPARATOR, \get_include_path()), '/PhixCommands.*\.php$/');

try
{
$files = $classFinder->getClassFiles();
}
catch(ClassConflictException $e)
{
$this->context->stderr->output($this->context->errorStyle, $this->context->errorPrefix);
$this->context->stderr->outputLine(null, $e->getMessage());

$files = $classFinder->getClassFiles();
}

foreach ($classFinder->getClassFiles() as $newClass => $filename)
foreach ($files as $newClass => $filename)
{
include_once $filename;

Expand Down
2 changes: 1 addition & 1 deletion src/php/Phix_Project/Phix/Phix.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function processPhixSwitchesBeforeExtensionLoad(Context $context, Pars
protected function loadPhixExtensions(Context $context, ParsedSwitches $ParsedSwitches)
{
// create something to find the commands
$extensionsFinder = new ExtensionsFinder();
$extensionsFinder = new ExtensionsFinder($context);

// seed the commandsFinder with a list of where to look
// if the user has given us any hints
Expand Down